无法在Core Data中创建只读sqlite存储:Cocoa Error 260 [英] Unable to create a readonly sqlite store in Core Data: Cocoa Error 260

查看:142
本文介绍了无法在Core Data中创建只读sqlite存储:Cocoa Error 260的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用readonly选项( NSReadOnlyPersistentStoreOption )创建一个 NSSQLiteStoreType 。如果sqlite文件不存在,这将失败(见下面的代码)。如果它存在,则添加商店而没有任何错误。

I'm trying to create a NSSQLiteStoreType with the readonly option (NSReadOnlyPersistentStoreOption). This fails if the sqlite file doesn't exist (see code below). If it does exist, the store is added without any errors.

我得到的错误是 Cocoa错误260

NSFileReadNoSuchFileError = 260,     // Read error (no such file)

所以看起来CoreData试图读取一个不存在的文件,而不是创建一个新的...

So it looks like CoreData tries to read a file that doesn't exist, instead of creating a new one...

似乎添加 NSReadOnlyPersistentStoreOption 您只能打开以前存在的存储,但不能创建一个。这对我来说没有意义。

It seems that when adding NSReadOnlyPersistentStoreOption you can only open a previously existing store, but not create one. This doesn't make sense to me.

有没有办法在Core Data中创建一个全新的只读存储?

Is there any way to create a brand new readonly store in Core Data?

如果没有,是否有一些解决方法?

If not, is there some workaround?

// DB URL
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *dbURL = [[fm URLsForDirectory:NSDocumentDirectory
                           inDomains:NSUserDomainMask] lastObject];
dbURL = [dbURL URLByAppendingPathComponent:@"store.sqlite"];


// Object Model
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
NSAssert([fm fileExistsAtPath:[modelURL path]], @"File not found");

NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

// Store Coordinator
NSPersistentStoreCoordinator *coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

// Add a readonly SQLite store
NSError *err = nil;
NSPersistentStore *store = [coord addPersistentStoreWithType:NSSQLiteStoreType
                                               configuration:nil URL:dbURL
                                                     options:@{NSReadOnlyPersistentStoreOption : @YES}
                                                       error:&err];
if (store == nil) {
    // I get a Cocoa Error 260.
    NSLog(@"Error: %@", err);
}


推荐答案

只读存储没有意义,你看到的结果正是预期的结果。通过指定只读,你具体表明没有文件应该被写入,因此....没有文件被写入。

Creating a new, empty, read-only store makes no sense, and the results you're seeing are exactly what would be expected. By specifying read-only you are specifically indicating that no file should be written, so as a result.... no file is written.

很难说什么试图完成。如果文件 被创建,您将无法使用它,因为它将不包含任何数据,并且由于只读标志将阻止您添加任何数据。一个空文件是完全一样有用的。

It's hard to tell what you're trying to accomplish. If the file were created, you would not be able to use it, since it would contain no data and since the read-only flag would prevent you from adding any data. An empty file would be exactly as useful.

但是没有办法告诉Core Data创建一个新的永久存储文件,主要是因为这样的操作是无意义的和无用的。

But no, there is no way to tell Core Data to create a new persistent store file but have that file be read only, mainly because such an operation would be nonsensical and useless.

如果你有一些理由想要持久存储文件,它是空的和不可写的(如果你这样做,请共享),您需要

If you have some reason to want a persistent store file which is both empty and unwritable (and if you do, please share), you would need to


  1. 添加持久存储
  2. 调用 removePersistentStore:error:删除持久存储

  3. 只读标记。

  1. Add the persistent store without the read-only flag
  2. Call removePersistentStore:error: to remove that persistent store
  3. Add the persistent store again, with the read-only flag.

现在,您将拥有一个不包含数据的持久存储,阻止向其中添加数据。

You will now have a persistent store which contains no data, and which you are prevented from adding data to.

一个更简单的替代方法是同样有效的是不首先创建文件。空的只读持久化存储根本没有任何意义,所以简单的方法是只是不打扰创建它。

A simpler alternative that is just as effective is to not create the file in the first place. An empty read-only persistent store serves literally no purpose at all, so the easy approach is to just not bother creating it.

这篇关于无法在Core Data中创建只读sqlite存储:Cocoa Error 260的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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