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

问题描述 投票:0回答:1

在本教程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> Clean几次,这不是问题

来自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"]);

}
ios objective-c core-data
1个回答
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文件复制到新项目并获取。我认为它会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.