1

I've been using MagicalRecord quite a bit lately - and blimey it's been making my Core Data life much, much easier, so full credit to the author of what is a quality library!

Anyway, the scenario is I have navigation controller stack of 3 controllers, referred here from bottom to top as A-B-C:

  • View controller A is a form where I would like to fill in the details about the entity (i.e., its properties).
  • View controller B shows the result of a calculation from the entity (e.g., via an instance function).
  • For the sake of simplicity, view controller C is just a confirmation & is where I'd like to save the entity.

Is there a way in MagicalRecord I can call [MyEntity createEntity] in view controller A with its properties set, pass it through to C via B & only save it in C? This would also include the possibility of not saving it at all should the user decide to go back to A from B or C.

I fully appreciate I may well be getting the wrong end of the stick with Core Data & it may not be possible. As a workaround, already I know I can create a class method that does the same calculation given the relevant parameters & pass all the parameters through the stack from A to C.

Edit: Just to make it clear, I want to call [[NSManagedObjectContext defaultContext] save] in View Controller C.

Simon Rice
  • 1,119
  • 1
  • 13
  • 22

2 Answers2

1

yes sure.. just dont save the managedContext.

the VCs should run all on the main thread anyways.. so all can use

[NSManagedObjectContext defaultContext]

OR

pass the MOC between the three classes using a property on the controllers.

@property NSManagedObjectContext *context;
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • It's good, but unfortunately it's not the right answer. If I create an entity (labelled p) in View Controller A, go to B to view the result of calculation on the instance method. I then decide to go back to A & create another new entity (labelled q), go through to C via B, where I call `[[NSManagedObjectContext defaultContext] save]`. If I go through that scenario, unfortunately *both* p & q are saved. However, you do get an upvote for having the right idea. – Simon Rice Nov 30 '12 at 12:42
1

After some testing, I knew @Daij-Djan had the right idea in that I don't call [[NSManagedObjectContext defaultContext] save] if I don't want to save my entity. As a result I leave that call until View Controller C to do the saving.

However, my testing showed I need to do a little more to avoid saving any unwanted entities. I've noticed if I go to A from B via the back button, I want to discard the entity there. In my use-case, it doesn't matter if I just create another new entity going from A to B, and I never pass back through View Controller B if I have successfully saved the entity.

So basically I need to delete the unsaved entity if the back button is pressed on View Controller B. This answer helps me massively there, resulting in this code in View Controller B:

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
        // self.entity is the instance of my entity

        [self.entity deleteEntity];
        self.entity = nil;
    }
    [super viewWillDisappear:animated];
}
Community
  • 1
  • 1
Simon Rice
  • 1,119
  • 1
  • 13
  • 22