Multicore / Thread im Importer

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Multicore / Thread im Importer

    hallo zusammen


    ich habe eine konsolen-app, welche mir csv dateien in meine coredata db importiert.


    da das ganze relativ lange dauert und ich immer wieder mal einen neuen import machen muss, würde ich den bereich, welcher die relations generiert in ein thread auslagern.


    er macht mir das, jedoch kommt er nicht mehr aus loop heraus.


    PHP-Quellcode

    1. NSFetchRequest *leistungRequest = [[NSFetchRequest alloc] init];
    2. [leistungRequest setEntity: [NSEntityDescription entityForName: @"LEISTUNG" inManagedObjectContext: context]];
    3. NSArray *leistungResult = [context executeFetchRequest:leistungRequest error:nil];
    4. long count = [leistungResult count];
    5. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    6. dispatch_sync(queue, ^(){
    7. for (size_t i = 0; i < count; ++i){
    8. dispatch_async(queue, ^(){
    9. [self relationsLEISTUNG: [leistungResult objectAtIndex: i]];
    10. });
    11. }
    12. });
    Alles anzeigen




    über einen hinweis würde ich mich freuen.


    viele grüsse


    phil
  • Kannst Du mehr Hinweise zu Deinem Codeschnipsel geben? Was mach relationsLEISTUNG genau? Beachte auch, dass Du für jeden thread einen eigenen moc benötigst. In Deinem Beispiel verwendest Du MOs, die Du von Deinem fetchRequest zurück bekommen hast in einem anderen thread (der operation Queue). (beachte auch, dass Du keine Queue mit hoher Priorität benötigst).

    The pattern recommended for concurrent programming with Core Data is thread confinement: each thread must have its own entirely private managed object context.

    There are two possible ways to adopt the pattern:

    Create a separate managed object context for each thread and share a single persistent store coordinator.
    This is the typically-recommended approach.

    Create a separate managed object context and persistent store coordinator for each thread.
    This approach provides for greater concurrency at the expense of greater complexity (particularly if you need to communicate changes between different contexts) and increased memory usage.

    You must create the managed context on the thread on which is will be used. If you use NSOperation, note that its init method is invoked on the same thread as the caller. You must not, therefore, create a managed object context for the queue in the queue’s init method, otherwise it is associated with the caller’s thread. Instead, you should create the context in main (for a serial queue) or start (for a concurrent queue).

    Using thread confinement, you should not pass managed objects or managed object contexts between threads.--

    Quellcode

    1. NSOperationQueue *queue = [ [ NSOperationQueue alloc ] init ];
    2. [ queue addOperationWithBlock:^{
    3. NSManagedObjectContext *privateContext = // hier aus psc deiner app erstellen
    4. NSFetchRequest *leistungRequest = [[NSFetchRequest alloc] init];
    5. [leistungRequest setEntity: [NSEntityDescription entityForName: @"LEISTUNG" inManagedObjectContext:privateContext]];
    6. NSArray *leistungResult = [privateContext executeFetchRequest:leistungRequest error:nil];
    7. for ( id myMo in leistungResult ) {
    8. // do something very expensive with myMO
    9. }
    10. [ privateContext processPendingChanges ];
    11. }];
    12. // im mainthread dann auf NSManagedObjectContextObjectsDidChangeNotification lauschen
    Alles anzeigen

    Hier der Link zur Doku:
    developer.apple.com/library/io…ef/doc/uid/TP40003385-SW1

    Hoffe, das hilft erstmal weiter, beste Grüße, Markus
  • hallo markus
    so ich hab das jetzt endlich mal ausprobieren können und es funktioniert jetzt soweit. besten dank.

    wenn ich mir jedoch das programm in der aktivitätsanzeige anschaue, sehe ich nur 2 threads und die prozessorauslastung ist bei 100%. leider noch nicht das gewünschte resultat. da ich ein 8 kern rechner habe, wäre eine multicore importer natürlich schon.

    hättest du mir noch ein tipp?

    viele grüsse

    philippe