Skip to content

Difference between Context.root and Context.main and why should we use Context.main as a default context #8

Open
@naoyamakino

Description

@naoyamakino

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 an Entity and used as saving. but in the example Recipe, this way it prevents to save in a persistent data storage.

for example,

(main)> Recipe.new(:name => "this is from main")
=> <Recipe: 0xa181090> (entity: Recipe; id: 0xa17e720 <x-coredata:///Recipe/tEF6D2C4C-25D5-46BF-90FB-B2F3733FDC347> ; data: {
    image = nil;
    ingredients =     (
    );
    instructions = nil;
    name = "this is from main";
    overview = nil;
    prepTime = nil;
    thumbnailImage = nil;
    type = nil;
})
(nil)? MotionData::Context.main.save(Pointer.new(:object))
=> true
(main)> Recipe.all.to_a.count
=> 2
(main)> exit
#re open the app
(main)> Recipe.all.to_a.count
=> 1

but when I use Context.root

(main)> context = MotionData::Context.root
=> #<MotionData::Context:0x956eba0>
(nil)? recipe = Recipe.newInContext(context)
=> <Recipe: 0xa94e590> (entity: Recipe; id: 0xa94e5d0 <x-coredata:///Recipe/tD58E5327-A318-4A42-83B3-74BFAEF73CA67> ; data: {
    image = nil;
    ingredients =     (
    );
    instructions = nil;
    name = nil;
    overview = nil;
    prepTime = nil;
    thumbnailImage = nil;
    type = nil;
})
(main)> recipe.name = "this is from root"
=> "this is from root"
(main)> context.save(Pointer.new(:object))
=> true
(main)> Recipe.all.to_a.count
=> 2
(main)> exit
#exit an app and reopen it
(main)> Recipe.all.to_a.count
=> 2

By default, I would expect .new to create an in an context that can be saved in persistent storage, thus Context.root not Context.main or Context.current

so should new be like this?

#from lib/motion_data/managed_object.rb#28
      def new(properties = nil)
        newInContext(Context.root, properties)
      end

I might be misunderstanding the use of Context.root and Context.main, in fact I don't know the difference.

so a question is, should it be using Context.root instead of Context.main at ManagedObject#new, and if it needs to use Context.main, then how can you do saving in a persistent way?

thanks for your comment.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions