Passing core data object from one controller to another without saving

I’m trying to pass core data object from one controller to another without saving the changes. but I’m getting CoreData: error: Mutating a managed object 0x14f70f510

Here’s the code:

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

  NSManagedObjectContext *localContext = [NSManagedObjectContext MR_context];
  Item *item  = [Item MR_createEntityInContext:localContext];
  item.code = indexPath.row;

  NextController *controller = [[NextController alloc] initWithItem:item];
  [self.navigationController pushViewController:controller animated:YES];

The reason why I don’t want to save the data right away because I want to wait until the user finished the process of creating the new item.

I can do this with creating regular object but since I already have the data model with core data, it seems redundant create two models.

Thats ok because remember the MOC is a scratchpad which doesnt commit changes to the psc until you call save! So you can go ahead and move onto your next viewcontroller and the object in the moc will still be there.

So it’s safe to ignore the warning? CoreData: error: Mutating a managed object 0x14f70f510

No, what I meant was you normally should save to pack when your object is complete. You can build your object properties over the span of 2 or 3 view controllers. Just save at the end.

Right. If I set the object properties using the core data entity object, I’m getting the CoreData: error: Mutating a managed object 0x14f70f510. But the data was passed correctly. So is it safe to ignore these warning? At least until all the data are complete.

How are you moving “away” from the view controller? Is it being dismissed somehow?

Is this in some personal project or CoreData book example?

I moved away by pushing a new view controller., so it’s not dismissed.
[self.navigationController pushViewController:controller animated:YES];

Is it possible you have a code that deleteObject’s the object by mistake or creates 2 MOCs?

1 Like

damn, that was it. I created 2 MOCs. Thanks for your help!
By any chance, are you familiar with creating autoresize & autolayout collectionview?

Thanks man!