我可以在没有特定的NSManagedObjectContext中创建NSManagedObject的实例吗? [英] Can I make instances of an NSManagedObject in no particular NSManagedObjectContext?

查看:97
本文介绍了我可以在没有特定的NSManagedObjectContext中创建NSManagedObject的实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个应用程式,可从网页API接收大量商家资讯,并允许使用者储存一些离线观看清单。我通常的方法是:

I'm building an app which receives a number of listings from a web API, and allows the user to save some for offline viewing. My usual approach would be:


  1. 从API获取数据,并为每个数据创建一个新的Listing对象



$ b

但是这是一个Core Data应用程序,所以上下文是什么得到保存,而不是对象。在这种情况下,上面的代码将变成这样:

But this is a Core Data app, so the context is what gets saved, not the object. Under those circumstances, the above would become something like this:


  1. 从API获取数据,并为每个数据创建一个非托管的Listing对象

  2. 如果用户选择将对象移动到托管上下文中,则保存上下文

有一个非托管和托管版本的Listing对象的方法是有两个类,例如ManagedListing和UnmanagedListing - 但这是一个可怕的重复的做法。

One approach to having an unmanaged and a managed version of Listing objects would be to have two classes, e.g. ManagedListing and UnmanagedListing - but that's a horribly repetitive way of doing it.

我想要的是使列表NSManagedObject的子类;初始化一堆它们没有NSManagedObjectContext;那么当我想保存一个时,我要么设置它的上下文,要么将其属性复制到上下文中的一个新的列表中。

What I'd like is to make Listing a subclass of NSManagedObject; initialise a bunch of them without an NSManagedObjectContext; then when I want to save one, I either set its context or I copy its attributes to a new Listing inside a context.

我可以在一个NSManagedObject实例特殊NSManagedObjectContext?如果是,如何?

Can I make instances of an NSManagedObject in no particular NSManagedObjectContext? If so, how?

推荐答案

使用两个永久存储,一个在内存中,一个在磁盘上。如果用户想保存,使用 assignObject:toPersistentStore:将对象移动到其他存储。

Use two persistent stores, one in memory and one on disk. If the user wants to save, move the object to the other store using assignObject:toPersistentStore:.

这篇关于我可以在没有特定的NSManagedObjectContext中创建NSManagedObject的实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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