Re: Core Data - Importing using NSOperation - Problems on main thread
Re: Core Data - Importing using NSOperation - Problems on main thread
- Subject: Re: Core Data - Importing using NSOperation - Problems on main thread
- From: Quincey Morris <email@hidden>
- Date: Sun, 20 Dec 2009 11:36:28 -0800
On Dec 20, 2009, at 09:16, Nick Banks wrote:
> Yes I did use the [managedObjectContext save:&error], in the same fashion as in some of the examples provided by Apple. Interestingly, I am using the same mechanism in a completely unrelated project with no problems, and have studied the Core Data documentation for some time.
It sounds like you're Doing It Wrong™. Here's what the Core Data docs have to say about multi-threaded access:
"A persistent store coordinator provides to its managed object contexts the façade of one virtual store.
For completely concurrent operations you need a different coordinator for each thread."
and:
There are three patterns you can adopt to support multi-threading in a Core Data application; in order of preference they are:
"• Create a separate managed object context for each thread and share a single persistent store coordinator.
If you need to “pass” managed objects between threads, you just pass their object IDs.
"If you want to aggregate a number of operations in one context together as if a virtual single transaction, you can lock the persistent store coordinator to prevent other managed object contexts using the persistent store coordinator over the scope of several operations.
"• Create a separate managed object context and persistent store coordinator for each thread.
If you need to “pass” managed objects between threads, you just pass their object IDs.
"Using a separate persistent store coordinator for each thread allows for completely concurrent operations."
[etc]
Deleting objects that are actively being fetched and/or displayed sure sounds like a "completely concurrent" operation. Sounds like you need a separate persistent store controller for each thread. (And even then you might have to deal with concurrency issues in your user interface, but that's a general multithreading headache, not Core Data specific.)
> If I delete an object in the import moc (moc2) will that immediately cause the object in moc1 to be deleted also (thus causing problems when referenced by the UI i.e. it will no longer exist) or will this only happen when I call [managedObjectContext save:&error]?
Deletions occur in the persistent store, not in the managed content. When you "delete" an object in a MOC, it's merely flagged so that the underlying data is removed from the store when the next save occurs. Neither the store nor any other MOC knows about the deletion until at least then.
Nothing can "cause the object in moc1 to be deleted" because the objects (the in-memory Objective-C objects) aren't deleted at all. Instead they are dealloc'ed or finalized when no longer referenced, according to the normal memory management rules. However, still-referenced objects that correspond to a saved persistent store deletion (the scenario you describe) will presumably produce errors if you cause fetching of their properties to be attempted.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden