0

关于“不访问”sqlite DB 的说明:尽管 NSBundle 具有 .sqlite 并且 .sqlite 的文件大小是正确的大小,但上下文/存储无法仅在设备上检测 .sqlite 中的任何条目。在模拟器中,上下文/存储工作正常。

我对此感到很困惑。我在我的 xcdatamodeld 中添加了一个模型版本。我重置了版本,让我的代码自动进行迁移,并重新填充了我的数据库。

在 iPhone 模拟器上,它可以工作。但是,当从设备运行时,它无法从数据库中读取。

我已经输入代码来检测 sqllite 实际上在设备上,并验证文件大小。正在从 NSBundle 正确复制 SQLLite,以及指示存在数据的文件大小。

不知何故,我的代码拒绝访问设备上的数据库。知道这是为什么吗?访问数据库的代码没有改变——唯一改变的是数据模型。下面的代码在升级之前适用于设备。

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
    return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
  if (_managedObjectModel != nil) {
    return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"sirona" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"sirona.sqlite"];

NSString *databasePath = [NSBundle.mainBundle pathForResource:@"sirona" ofType:@"sqlite"];
NSLog(@"File manager: %@", [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] bundlePath] error:nil]);
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath: @"sirona.sqlite" error: NULL];
UInt32 result = [attrs fileSize];
NSLog(@"File size: %d", (unsigned int)result);
NSError *error = nil;

// important part starts here
   NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,
                            [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                                           nil];

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _persistentStoreCoordinator;
}

我如何通过核心数据访问数据库:

  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:[NSEntityDescription entityForName:@"Representative" inManagedObjectContext:context]];
  [request setIncludesSubentities:NO]; //Omit subentities. Default is YES (i.e. include subentities)

  NSError *err;
  NSUInteger count = [context countForFetchRequest:request error:&err];
  NSLog(@"Count: %lu", (unsigned long)count);

编辑:不确定如果没有将其复制到设备的代码,这是如何工作的。但它现在似乎正在工作。

执行此操作的代码:

NSURL *storeURL = [[self applicationDocumentsDirectory]    URLByAppendingPathComponent:@"sirona.sqlite"];

if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {
    NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sirona" ofType:@"sqlite"]];
    NSLog(@"PreloadURL: %@", preloadURL);
    NSError* err = nil;

    if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err]) {
        NSLog(@"Oops, could copy preloaded data");
    }
}

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
4

1 回答 1

0

在初始化 CoreData 上下文之前,您必须将您的 sqlite 文件复制到您拥有读/写权限的文件夹中

于 2013-06-26T16:33:10.887 回答