New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MR_inContext: Thread Safety #448
Comments
What I do is pass along the NSManagedObjectContext and create different ManagedObjects for each thread... |
When you move NSManagedObject from one context to another one what do you pass? NSManagedObject or objectID? |
You cant pass the NSManagedObject because it's not thread safe (its tied) |
This is what the documentation says, take a look at MagicalRecord method MR_fetchInContext: they assume that you can pass the NSManagedObject (not the objectID) in another context and only in that moment do the fetch. |
Thread confinement or not, you cannot pass NSMOs across threads. The MR_inContext: method is how you transfer one object to another thread or queue. Now, technically, this method leads you to an incorrect access pattern in that your previous NSMO is from a different thread AND context. However, when you look at what's going on "under the covers" we're not accessing any Core Data properties at this point, we're accessing a ObjectID. Accessing the properties will lead to a crash, but asking for the objectID has been a safe operation. Now, if you want to do this truly in a thread safe manner, then I suggest doing something like this:
|
@casademora @tonyarnold Do either of you have a source as to why From core data documentation:
|
The documentation you've quoted is the the source you're looking for. I'm not quite sure what you're asking - I'm sure some of the older WWDC sessions around Core Data cover this in more detail, but I'm just not sure which ones now. |
The documentation says that
Here's another seemingly broken order:
The correct way to do this would be:
|
That's correct 👍 |
The recommended way to use NSManagedObject from one thread to another is to use thread confinement and pass the objectID. This is what Apple said in its documentation and what I read in almost all the internet guide about Core Data and thread confinement.
The method MR_inContext: assume that you can always access the objectID of a NSManagedObject without any problem.
I understand that this method can create problem with temporaryIds, but is it really safe to access the objectID of an NSManagedObject from a different thread?
Is there any documentation page talking about this argument?
Is it safe to use this approach also without context parenting, for example on 10.6?
The text was updated successfully, but these errors were encountered: