将内存中存储保存到iOS上的文件 [英] Save In-Memory store to a file on iOS

查看:421
本文介绍了将内存中存储保存到iOS上的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经使用以下方法建立了一个内存中核心数据存储:

Let's say I already setup a in-memory core data store using this:

- (NSPersistentStore *)addPersistentStoreWithType:(NSString *)storeType configuration:(NSString *)configuration URL:(NSURL *)storeURL options:(NSDictionary *)options error:(NSError **)error;

并节省了很多东西。

现在,我要将所有存储存储到文件中。

Now I want to save all of this store into a file.

有可能吗?

我尝试了多种搜索,但似乎从未有人碰过这个问题。

I tried many kind of search but it looked like no one has ever bumped into this issue before.

推荐答案

无法将内存中的 NSPersistentStore 保存到文件中。它不符合 NSCoding 或提供任何能够实现此目的的方法,并且向其询问数据的唯一方法是通常的核心数据获取方法

There's no way to just save the in-memory NSPersistentStore to a file. It doesn't conform to NSCoding or provide any methods of its own that would enable this, and the only way to ask it for its data is the usual Core Data approach of fetching it.

但是,您可以轻松地迁移到基于文件的存储。使用持久性存储协调器将基于内存的存储迁移到文件。 首先,请确保您没有任何仍在使用内存驻留存储的对象( NSManagedObjectContext NSManagedObject )。然后执行以下操作:

However, you can migrate to a file-based store pretty easily. Use your persistent store coordinator to migrate the memory-based store to a file. First make sure that you don't have any objects that still make use of the memory-resident store (NSManagedObjectContext, NSManagedObject). Then do something like:

NSURL *fileURL = // set this up to have the URL of the file you want
NSError *error = nil;
NSPersistentStore *fileStore = [self.persistentStoreCoordinator
    migratePersistentStore:self.memoryResidentStore
    toURL:fileURL
    options:nil
    withType:NSSQLiteStoreType
    error:&error];

执行此操作后,旧的持久性存储( self.memoryResidentStore 不再有用。

Once you do this, the old persistent store (self.memoryResidentStore in the code above) is no longer useful.

这篇关于将内存中存储保存到iOS上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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