如何使用Core Data模型而不保存? [英] How to use Core Data models without saving them?

查看:124
本文介绍了如何使用Core Data模型而不保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个应用程序,我使用MagicalRecord作为与Core Data交互的框架。应用程序从服务器获取海报数组,然后显示它们。

I'm writing an application and I am using MagicalRecord as a framework for interacting with Core Data. The application fetches an array of posters from a server and then displays them. Posters can also be created on the app and then uploaded to the server if the user requires it.

因此,用户创建的海报可以使用Core Data存储在本地数据库中,而从服务器抓取的海报应该只显示在应用程序中,而不是本地保存。如何使用相同的Poster类(现在是NSManagedObject的子类)来处理这两种情况?

So posters created by the user are stored in the local db using Core Data, while posters fetched from the server should only be displayed in the app but not saved locally. How can I use the same Poster class (which now is a subclass of NSManagedObject) to handle both these cases?

这是我的类:

@interface Poster : NSObject
@property (nonatomic, retain) NSNumber * posterID;
@property (nonatomic, retain) NSString * artists;
@end

当我从服务器获取海报数组时,然后指定属性:

When I fetch the posters array from the server I allocate a new poster and then assign attributes:

Poster *poster = [[Poster alloc] init];
if ([dict objectForKey:@"id"]) poster.posterID = [dict objectForKey:@"id"];
if ([dict objectForKey:@"artists"]) poster.artists = [dict objectForKey:@"artists"];

但是当到达链接poster.posterID = [dict等等时,应用程序崩溃与此错误



But when reaching the linked poster.posterID = [dict etc etc the application crashes with this error


由于未捕获异常而终止应用程序'NSInvalidArgumentException',reason:' - [Poster setPosterID:]:无法识别的选择器发送到实例0xaa8b160'

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Poster setPosterID:]: unrecognized selector sent to instance 0xaa8b160'

如果我使用 Poster * poster = [Poster createEntity]; 创建新对象 Poster * poster = [[Poster alloc] init]; ,应用程序不会崩溃,但是当我保存上下文时,我发现从服务器获取的所有海报

If I create the new object with Poster *poster = [Poster createEntity]; instead of Poster *poster = [[Poster alloc] init];, the app doesn't crash, but when I save the context I find all the posters fetched from the server are saved locally.

我如何解决这个问题?

推荐答案

不能只是 alloc / init 一个托管对象,因为托管对象必须与托管对象上下文相关联。 poster.posterID = ... 崩溃,因为动态创建的访问器方法在没有受管对象上下文的情况下不工作。 (更正:正如@noa正确地说,你可以创建没有托管对象上下文的对象,只要你使用指定的初始化器,但这些对象对任何获取​​请求都不可见)。

You cannot just alloc/init a managed object, because a managed object must be associated with a managed object context. poster.posterID = ... crashes because the dynamically created accessor methods do not work without a managed object context. (Correction: As @noa correctly said, you can create objects without a managed object context, as long as you use the designated initializers. But those objects would not be "visible" to any fetch request.)

要创建不应保存到磁盘的受管对象,您可以使用两个永久存储:一个SQLite存储和一个单独的内存存储。

To create managed objects that should not be saved to disk you can work with two persistent stores: one SQLite store and a separate in-memory store.

我不能告诉你如何使用MagicalRecord,但是使用普通核心数据,它将工作如下:

I cannot tell you how to do that with MagicalRecord, but with "plain Core Data" it would work like this:

创建托管对象上下文和持久核心协调器,您可以向商店协调员分配两个持久性商店:

After creating the managed object context and the persistent core coordinator, you assign two persistent stores to the store coordinator:

NSPersistentStore *sqliteStore, *memStore;

sqliteStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error];
if (sqliteStore == nil) {
    // ...
}
memStore = [coordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&error];
if (memStore == nil) {
    // ...
}

稍后,当您将新对象插入上下文时,您可以将新对象与SQLite存储或内存存储关联:

Later, when you insert new objects to the context, you associate the new object either with the SQLite store or the in-memory store:

Poster *poster = [NSEntityDescription insertNewObjectForEntityForName:@"Poster" inManagedObjectContext:context];
[context assignObject:poster toPersistentStore:memStore];
// or: [context assignObject:poster toPersistentStore:sqliteStore];
poster.posterID = ...;
poster.artists = ...;

只有分配给SQLite存储的对象保存到磁盘。如果重新启动应用程序,分配给内存存储的对象将会消失。我认为未明确分配给商店的对象会自动分配给第一个商店,在这种情况下这将是SQLite商店。

Only the objects assigned to the SQLite store are saved to disk. Objects assigned to the in-memory store will be gone if you restart the application. I think that objects that are not assigned explicitly to a store are automatically assigned to the first store, which would be the SQLite store in this case.

我还没有使用MagicalRecord,但我看到有方法 MR_addInMemoryStore MR_addSqliteStoreNamed 是此配置的适当方法。

I haven't worked with MagicalRecord yet, but I see that there are methods MR_addInMemoryStore and MR_addSqliteStoreNamed, which would be the appropriate methods for this configuration.

这篇关于如何使用Core Data模型而不保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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