forked from magicalpanda/MagicalRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
Accessing Entities Across Threads
tonyarnold edited this page Dec 14, 2012
·
1 revision
MagicalRecord can help simplify accessing entities across multiple threads. Here's one way to do it:
NSManagedObject *objectOnThreadOne = ...; // I'll assume you already have this object and need to use it on another thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSManagedObject *objectOnThreadTwo = [objectOnThreadOne MR_inThreadContext];
// Alternately, if you already have a managed object context on your background thread that you're working with:
NSManagedObject *objectOnThreadTwo = [objectOnThreadOne MR_inContext:myThreadManagedObjectContext];
});
What they're doing behind the scenes is getting the original NSManagedObject instance's NSManagedObjectID via the objectID
method on NSManagedObject. NSManagedObjectID's are safe to pass between threads, so you can use it in conjunction with something like NSManagedObjectContext's existingObjectWithID:error:
method.
Note: Expand on this, and simplify the language. Provide more detailed examples of doing it manually with Core Data, then show the simpler MagicalRecord methods.