开发者

CoreData and Shared ManagedobjectContext in TabBar iphone app

开发者 https://www.devze.com 2023-01-07 20:33 出处:网络
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\

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 a开发者_Python百科s 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,


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消