views:

315

answers:

1

I a, working on coredata iphone TabBar app and passing appdelegate managedObjectContext to all the views and subviews.At some instance i need to create new record for entity A and while entity A didn't saved by the user i need to create another entity B by using the same shared context. Now, my problem is if i save the context for entity B, then it will save the entity A as well. I don't want to save entity A until or less user didn't want to save it.

I can see only one way to go is creating separate context for entity B or something else....?

Please help me to slove this issue.

Thanks,

A: 

You can:

  1. Use a separate context. Pro: Does what you want. Con: Added complexity and you can't easily form relationships.
  2. Create object-A in a nil context by passing a nil value for the context when you insert the object. Then add it to the context when you want to save. Pro: Does what you want. Con: Same as above and loose all the functionality provided by the context such as undo support.
  3. Just delete object-A is the user decides they don't want to keep. Pro: Simple and safe. Con: The relationships in the object graph have to allow the object-A to be deleted i.e. other objects you want to keep cannot require object-A to exist. You have to keep this in mind when designing the model.

I almost always go with (3). It's the simplest and safest method by far. Since it mimics what the user is actually doing i.e. creating a data object and then deciding to throw it away, it is good design practice as well.

TechZen
Thanks for your help. I understand your suggestions, but in my case i need to give Look-up and allow user to add new related entities on the fly while adding new record for Object A. As i mention before Object A is the master table contains other child tables references. So, while adding new record in Master table A, user should able to select child table records and also able to create new if doesn't exists.My problem is first i am creating the new Object A and then allowing user to add new child object B on the fly. For this i found the solution 1 (separate context) working in my case.
AmitSri