是否可以在核心数据中以只读方式返回NSManagedObjects? [英] Is it possible to return NSManagedObjects as read-only in Core Data?

查看:55
本文介绍了是否可以在核心数据中以只读方式返回NSManagedObjects?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Core Data在正在编写的模块中缓存服务器数据,并希望能够强制应用程序的其他部分不修改该数据.

I'm using Core Data for caching of server data in a module that I'm writing and would like to be able to enforce that other parts of the application don't modify that data.

是否可以将我的NSManagedObject实例或用于获取它们的NSManagedObjectContext实例标记为只读?由于可以直接从对象访问上下文,因此消费者可以自由地修改对象并调用 [obj.managedObjectContext save:& errror] .

Is is possible to mark either my instances of NSManagedObject, or the NSManagedObjectContext used to fetch them, as read-only? Since the context is accessible directly from the object, consumers are free to modify the object and call [obj.managedObjectContext save:&errror].

我已经看到很多有关创建完全只读存储的参考(例如:

I've seen plenty of references to creating a completely read-only store (like here: Unable to create a readonly sqlite store in Core Data: Cocoa Error 260), but I don't want the NSPersistentStore itself to be read-only (the module I'm writing should be able to write to it).

一个选项是将托管对象转换为具有相同属性的普通NSObject子类.但是后来我失去了Core Data可以完成的一些不错的事情,例如延迟加载等.

One option is to convert the managed objects into plain NSObject subclasses with the same properties. But then I lose some of the nice things like lazy loading, etc, that Core Data can do.

有什么想法吗?

推荐答案

NSManagedObject 本身不支持只读状态,因此这不是一件小事.不过,您可以做一些稍微有些骇人听闻的事情.

NSManagedObject doesn't support a read-only state on its own, so it's not a trivial matter. There are some slightly-hackish things you can do to get something like you describe, though.

  • 如果您使用的是 NSManagedObject 的自定义子类,则可以将布尔 readOnlyMode 属性添加到该类(而不是实体).然后,您可以在执行任何操作之前重写setter方法以检查此属性.您将获得按对象的只读行为,但是没有什么可以阻止应用程序其他部分仅更改该值的.
  • 您可以对 NSManagedObjectContext 本身执行类似的操作.创建您自己经常使用的子类,并添加一个类似的 readOnlyMode 属性,您可以在调用 save:时检查该属性.但是任何人都可以更改该标志.
  • If you're using custom subclasses of NSManagedObject, you could add a Boolean readOnlyMode property to the class (not to the entity). Then you could override the setter methods to check this property before doing anything. You'd get per-object read-only behavior, but there's nothing to stop other parts of the app from just changing that value.
  • You could do something similar on the NSManagedObjectContext itself. Create your own subclass, which you always use, and add a similar readOnlyMode property that you check when save: is called. But anyone could change that flag.

或者您可以用苛刻的方式做到这一点.在您自己的控制器类之一中使用布尔 readOnly 变量.然后收听 NSManagedObjectContextObjectsDidChangeNotification .如果该通知在只读模式下到达,则使应用程序崩溃.如果该应用程序的其余部分表面上都是由与您同一个团队的人员开发的,那么这应该是完全合理的,因为它只会在应用程序开发期间在内部进行.

Or you could do it the harsh way. Use a Boolean readOnly variable in one of your own controller classes. Then listen for NSManagedObjectContextObjectsDidChangeNotification. If that notification arrives during read-only mode, crash the app. If the rest of the app is being developed by people ostensibly on the same team as you, this should be entirely reasonable since it'll only happen in-house during app development.

这篇关于是否可以在核心数据中以只读方式返回NSManagedObjects?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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