Multithreaded Core Data Application

I try to use master data in several ways. I just want to show the application with previously loaded data while loading new data in the background. This should allow the user to access the application during the upgrade process.

I have an NSURLConnection that downloads a file asynchronously using a delegate (and shows progress), then I use XMLParser to parse the new data and create new NSManagedObjects in a separate context with my own persistentStore and using a separate stream.

The problem is that when creating new objects in the same context as the old one, a BAD_INSTRUCTION exception may be thrown when it is displayed. So, I decided to use a separate context for the new data, but I can’t figure out how to move all objects to another context after completion.

Paolo aka SlowTree

+54
multithreading ios iphone core-data
Jan 26 '10 at 8:40
source share
3 answers

The Concurrency Apple with documentation for master data is the place to start. Read this very carefully ... I was bitten many times by my misunderstandings!

Basic Rules:

  • Use one NSPersistentStoreCoordinator for each program. You do not need them in a stream.
  • Create one NSManagedObjectContext for each thread.
  • Never pass an NSManagedObject in a stream to another stream.
  • Instead, get the object identifiers through -objectID and pass them to another thread.

Additional rules:

  • Before you get the identifier of an object, save the object in storage. Until they are saved, they are temporary, and you cannot access them from another stream.
  • And be careful with merge policies if you make changes to managed objects from more than one thread.
  • NSManagedObjectContext -mergeChangesFromContextDidSaveNotification:

But let me repeat, please read the document carefully! It's really worth it!

+137
Jan 26 '10 at 9:02
source share

Currently, [May 2015], Apple Concurrency with documentation on master data is at best very misleading, as it does not cover any improvements in iOS 5 and therefore no longer shows the best ways to use the kernel data at the same time. There are two very important changes in iOS 5: the parent context and the new types of concurrency / threading.

I have not yet found written documentation that comprehensively covers these new features, but WWDC 2012 video β€œSession 214 - Key recommendations on the best data” explains all this very well.

Magical Record takes advantage of these new features and may be worth the look.

The real basics are the same: you can still use only managed objects into which a stream was created, controlled by the object context.

Now you can use [moc performBlock:] to run the code in the desired thread.

No need to use mergeChangesFromContextDidSaveNotification: more; instead, create a child context for changes, and then save the child context. Saving the child context will automatically introduce the changes into the parent context, and to save the changes to disk, simply save to the parent context in it.

To do this, you must create a parent context with a parallel type, for example:

 mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; 

Then in the background thread:

 context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType]; [context setParentContext:mainManagedObjectContext]; <... perform actions on context ...> NSError *error; if (![context save:&error]) { <... handle error ...> } [mainManagedObjectContext performBlock:^{ NSError *e = nil; if (![mainContext save:&e]) { <... handle error ...> } }]; 
+73
Aug 05 '12 at 8:21
source share

I hope this helps all peoples who face challenges using basic data in a multi-threaded environment.

Take a look at "Best Songs 2" in the apple documentation. Using this code, I took the Matrix β€œred pill” and discovered a new world without a double error and without errors .: D

Hope this helps.

Paolo

ps Thank you so much Yuji, in the documentation described above, I found this example.

+2
Feb 09 '10 at 12:16
source share



All Articles