将sqlite文件从一个项目复制到另一个项目

时间:2014-04-17 23:17:36

标签: ios objective-c core-data

按照本教程http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated,我创建了一个空的命令行应用程序,使用核心数据来为我随后移动的数据库(通过将quizgeodata.sqlite文件)移动到我正在构建的ios应用程序。教程列出的唯一其他步骤是将此代码添加到app delegate中的persistantStoreCoordinator

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

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

(之前的步骤包括将类文件(对应于实体)和... xcdatamodel.d文件放入空命令行应用程序以确保模式相同)。当我在空命令行应用程序中运行代码时,它显示我在日志语句中导入的获取数据,因此我假设将quizgeodata.sqlite复制回ios应用程序并在其中添加一位代码持久性存储协调器,然后以下代码(在viewDidLoad中)应该从核心数据中获取数据,但是日志语句显示没有数据被检索。下面代码中的错误日志表示'null'(意味着我没有假设错误),当我要求xCode打印sql语句时,它会在控制台中显示它

2014-04-17 16:11:57.477 qbgeo[767:a0b] CoreData: annotation: total fetch execution time: 0.0026s for 0 rows.

很明显,我复制的sqlite文件中没有数据。你能解释一下我可能需要做些什么才能让它发挥作用吗?

请注意我运行了Project>清洁几次,这不是问题

来自ViewDidLoad的

    id delegate = [[UIApplication sharedApplication] delegate];     self.managedObjectContext = [delegate managedObjectContext];

NSError *error;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Sportsquiz" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSLog(@"error %@", [error description]);
NSArray *messedUpObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (Sportsquiz *info in messedUpObjects) {
    NSLog(@"correctAnswer %@", [info valueForKey:@"question"]);
    NSLog(@"correctAnswer %@", [info valueForKey:@"correctAnswer"]);

}

1 个答案:

答案 0 :(得分:1)

试试吧:

原始项目:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}

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

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary];

/*ATTETION: disable WAL mode*/
[pragmaOptions setObject:@"DELETE" forKey:@"journal_mode"];
NSNumber *optionYes = [NSNumber numberWithBool:YES];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                         pragmaOptions, NSSQLitePragmasOption,
                         optionYes,NSMigratePersistentStoresAutomaticallyOption ,nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
abort();
}
return _persistentStoreCoordinator;
}

将sqlite文件复制到新项目并获取。我认为它会奏效。