我正在使用coredata和sqlite数据库来将数据保存在我的应用程序中。但是,每次我杀了我的应用程序,我都会丢失保存在数据库中的任何数据。我很确定它是因为我的数据库的.sqlite文件只是在每次我的应用程序启动时被一个新的.sqlite文件替换,但我似乎无法找到任何只使用现有的代码的代码。 如果任何人都可以指向我一些可以为我处理这个问题的代码,那将会很棒。iOS 5 - Coredata Sqlite数据库在丢失应用程序后丢失数据
干杯
乙
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"FlickrCoreData.sqlite"];
NSError *error = 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;
}
每次写入商店时都必须使用相同的ManagedObjectContext。你如何将数据写入moc以及如何将ManagedObjectContext传递给其他控制器。 – 2012-03-18 20:16:30