开发者

Building a list of favorites from Core Data

开发者 https://www.devze.com 2022-12-28 22:24 出处:网络
I\'m building an app with two tabs. The first tab has a main tableview that connects to a detail view of the row. The second tab will display a tableView based on the user adding content to it by tapp

I'm building an app with two tabs. The first tab has a main tableview that connects to a detail view of the row. The second tab will display a tableView based on the user adding content to it by tapping a button on the detail view.

My question is this. What开发者_运维问答 is the correct design pattern to do this? Do I create a second ManagedObjectContext/ManagedObjectContextID then save that context to a new persistent store or can the MOC be saved to the existing store without affecting the original tableview?

I've looked at CoreData Recipes and CoreData Books and neither deal with multiple stores although books does deal with multiple MOC's. Any reference would be great.


A single NSManagedObjectContext is more than sufficient for this design. What you want to do is implement the dependency injection design pattern. What this translates to is that when you create each of your tabs, you pass the singular NSManagedObjectContext instance into each of them. Each NSViewController is then responsible for accessing the NSManagedObjectContext as needed.

update

If you are seeing the same data then that is an issue with your 'NSFetchedResultsController' and not the 'NSManagedObjectContext'. The 'NSManagedObjectContext' has access to all of the data, the 'NSFetchedResultsController' is what does all of the filtering based on it's 'NSFetchRequest'.

Perhaps you should post the 'NSFetchedResultsController' for each of your controllers (by editing your question here) so that we can see what is going on.

update

The MOC does not get destroyed, at all, ever. You simply have more than one reference/pointer to the same MOC. The MOC is the scratchpad for the NSManagedObject instances that you are accessing. When you call -save: on that MOC, it takes the changes in that scratchpad and persists them out to disk.

Except in some very, highly unusual, situations, you only ever need one MOC per thread. In your design that you have described so far, one MOC is more than sufficient.

0

精彩评论

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