核心数据单例经理? [英] Core Data singleton manager?

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

问题描述

什么技术原因是没有单独的类来管理我的核心数据?我现在想做一个决定,如果我应该删除所有的样板核心数据代码,并重新实现它在一个单例。

What technical reasons are there not to make a singleton class to manage my Core Data? I'm trying to make a decision now, if I should strip out all of the boilerplate core data code and re-implement it in a singleton.

推荐答案

Xcode模板中应用程序委托中的样板代码在功能上实现为单例。应用程序对象是一个单例,它维护一个委托对象,所以你只有一个Core Data栈的实例,并且由于应用程序对象是普遍可访问的,所以你总是可以访问应用程序委托。

The boilerplate code in the application delegate in the Xcode templates is functionally implemented as a singleton. The application object is a singleton and it maintains but one delegate object so you've only got one instances of the Core Data stack and since the application object is universally accessible, you can always get to the app delegate as well.

然而,即使这只适用于具有一个持久存储的简单应用程序,所有上下文使用该一个存储。在更复杂的应用程序中,您可能有多个商店或上下文,因此单例快速变得过于。肿。

However, even that works only for simple apps with one persistent store with all the context using that one store. In more complex apps you may have multiple stores or context so a singleton quickly becomes too bloated.

单例通常不会为您购买复杂度隐藏或保存重复的代码,因为大多数与Core Data相关的编码都在控制器层将模型添加到视图/接口。由于这个逻辑通常是每个视图的自定义,你不能实际停放在singleton。

A singleton usually won't buy you much complexity hiding or save duplicate coding because most of the coding you have to do with Core Data is in the controller layer where you link up the model to the view/interface. Since that logic is usually custom to each view, you can't actually park it in the singleton.

我在过去使用单身人士,但最终他们通常比他们更值得拥有。

I've used singletons in the past but in the end they usually prove more hassle than they are worth.

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

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