-
Notifications
You must be signed in to change notification settings - Fork 23
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
Difference between Context.root and Context.main and why should we use Context.main as a default context #8
Comments
My understanding of the Context.root, Context.main and Context.context is that you should just use
and then do
and within that block do any .new of objects and it'll use the context you created. Another added benefit of doing it that way is that it'll create the right context depending on if you are in a background thread(or queue) or if you are on the main thread. Right before the end of the block do
And that'll save the objects into Context.main Next, do
to persist the objects to the database. BUT!!!!!!!, if your code is in a background thread (or queue) you should do something like
when you are saving. Bad things happen if you access a context from a thread that it wasn't created on. Only use the main and root contexts from the main thread. Oh, and here is the context save method that I actually use.
|
Thanks for helping out with this, David! Just to let you know, that in this simple case, you don't need to create a new context and use So, Naoya, here's what's happening. The problem comes down to saving in child and parent contexts. In Core Data you can set up contexts to be children of other contexts, and if you save in a child context, the changes get "bubbled up" to the parent context. However, the parent context hasn't persisted them to disk yet. You have to save the top parent context to actually persist the changes. This one-way relationship lets you alter the objects in memory in child contexts but then throw them away if you decide to cancel the operation. "Saving" a child context is all about bubbling changes up to the parent context. In MotionData, the So, in your example, saving the As I was investigating this, it turns out the Recipe demo app is broken because it doesn't properly save the parent contexts. When Eloy and I were last working on this, we weren't using the Recipe demo fully. We were just testing that defining a schema in Ruby code instead of a managed object definition file would work. Our test succeeded because the Recipe app boots up fine, but our testing didn't go farther beyond that yet. Let me know if you need more clarification on this. On Nov 27, 2012, at 9:22 AM, David Smith [email protected] wrote:
|
@jonathanpenn @jetpad thanks for the comments, much appreciate it. |
Merged it in and left a comment. Thanks! On Nov 27, 2012, at 1:41 PM, Naoya Makino [email protected] wrote:
|
Indeed, I got the inspiration from MagicalRecord and from WWDC talks. The general idea is this:
|
PS: We should add something like MagicalRecord’s |
Hi @alloy and @jonathanpenn, I am having a trouble understanding an use of different contexts and how to make a persistent save. By default it is using
Context.main
to do initialize anEntity
and used as saving. but in the example Recipe, this way it prevents to save in a persistent data storage.for example,
but when I use
Context.root
By default, I would expect
.new
to create an in an context that can be saved in persistent storage, thusContext.root
notContext.main
orContext.current
so should
new
be like this?I might be misunderstanding the use of
Context.root
andContext.main
, in fact I don't know the difference.so a question is, should it be using
Context.root
instead ofContext.main
atManagedObject#new
, and if it needs to useContext.main
, then how can you do saving in a persistent way?thanks for your comment.
The text was updated successfully, but these errors were encountered: