Kyle Fang
Kyle Fang

Reputation: 1199

Keep NSManagedObject outside of CoreData NSManagedObjectContext Block

I use the a helper class to get a block with NSManagedObjectContext in it.

+ (void)openTheDocumentAndPerformBlock:(completion_handle_t)completionBlock;

And now I want to keep one of the NSManagedObject outside of the block, because I want to interact with it constantly. And if I can't keep it in the ViewController, I have to query it everytime when I need it, and it will be really troublesome.

1, And can I pass the NSManagedObject through segue to another VC?
2, does the UIManagedDocument has to be open all the time when those thing append?
3, And Can I edit the NSManagedObject outside of the block, and save it in the Block?

Upvotes: 0

Views: 257

Answers (1)

Rakesh
Rakesh

Reputation: 3399

If I am understanding your question correctly, you just need an instance variable of type NSManagedObject in your class(the view controller class) so that you can assign your managed object, that you queried out in your block to this variable. You can use this wherever you want.
As for your other questions:
1. Hope the following code snippet works: In your destination view controller class declare an instance variable to store the passed object.

@property (strong) NSManagedObject *container;

inside the prepareForSegue: method of source view controller get the destination vc and pass the managed object:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    UIViewController *destVC = [segue destinationViewController];
    [destVC setValue:<managed object from block> forKey:@"container"]
}

And yes you can use NSManagedObject inside and outside your block as long its the same managed object context you are using.

And I don't know the answer for your second question.

Upvotes: 1

Related Questions