核心数据上下文和单例数据控制器 [英] Core Data context and singleton data controller

查看:67
本文介绍了核心数据上下文和单例数据控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单例数据控制器来保存一组对象.请参阅我的解决方案的香蕉问题示例:singeltondataController banansArray

I have a singleton data controller to hold an array of objects. See for example bananas question for my solution: singelton dataController banansArray

现在我想将香蕉数组保存到持久状态.本核心数据教程:核心数据 - 存储图像 让我对核心数据有了一个很好的总体了解,我能够在将数据控制器更改为单例之前将其包含在我的应用程序中.

Now I want to save the array of bananas to persistant state. This core data tutorial: core data - store images have given me a good general understanding of Core Data and I was able to include it in my application before changing my data Controller to singleton.

现在什么是最好的?

我是否需要将应用程序委托中生成的 Core Data 堆栈移动到管理香蕉数组的 singletonDataController?或者我是否必须像在使用 Core Data 模板生成的主视图控制器中一样在应用程序委托中设置单例的上下文?

Do I need to move the generated Core Data stack within the application delegate to the singletonDataController that manage the bananas array? Or do I have to set the context of the singleton in the application delegate as you do in the generated Master-View controller with Core Data template?

在这种情况下,我如何在 appDelegate 中设置上下文?这在 AppDelegate 应用程序中不起作用(它适用于模板中的 masterView)didFinishLaunchingWithOptions:

In that case how do I set the context in the appDelegate? This does not work (it workes for the masterView in template) in the AppDelegate application didFinishLaunchingWithOptions:

DataControllerSingleton *dataController;
dataController.managedObjectContext = self.managedObjectContext;

在 beerDataModel 示例中提供的 ManagedObjectCode 是:

In beerDataModel example provided the ManagedObjectCode is:

if (_mainContext == nil) {
    _mainContext = [[NSManagedObjectContext alloc] init];
    _mainContext.persistentStoreCoordinator = [self persistentStoreCoordinator];
}

推荐答案

根据您的问题,我认为这是个人选择.例如,在我的项目中,我更喜欢维护一个单例类来管理 Core Data 堆栈并在整个应用程序中使用它.我更喜欢让应用委托保持干净.

Based on your question, I think it's a personal choice. For example, within my project I prefer to maintain a singleton class for managing the Core Data stack and use it throughout the application. I prefer to leave the app delegate clean.

无论如何,现在如果您使用 Core Data,旧的单例,即管理对象数组的单例,将不再有用.使用 Core Data,您可以获得一个可以从持久存储中获取的对象图(例如).显然,您需要正确设计模型(实体、关系等).例如,在您的情况下,具有正确属性的 Banana 实体是正确的选择.

Anyway, now if you use Core Data the old singleton, the one that manages the arrays of objects, is not useful anymore. With Core Data you have a graph of objects that can be grabbed from a persistent store (for example). Obviously you need to design your model correctly (entities, relationships, etc.). In your case, for example, a Banana entity with the correct attributes is the right choice.

要查看单例类的实际效果,请查看 BeerDataModel.h/.m @BenSheirman.这是一个非常好的遵循方法.它可以像下面这样使用.

To see in action a singleton class take a look at BeerDataModel.h/.m by @BenSheirman. This is a very good approach to follow. It can be used like the following.

NSManagedObjectContext *mainContext = [[BeersDataModel sharedDataModel] mainContext];

附言更改 BananasDataModel 中的 BeersDataModel 或您喜欢的名称.

P.S. Change BeersDataModel in BananasDataModel or what name you prefer.

这篇关于核心数据上下文和单例数据控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆