所以我最近切换到了RestKit核心数据实现,并且在构建/应用程序启动之间存在一些持续存在数据的问题 - 即如果我创建一个对象并保存它,只要应用程序保持打开状态就可以正常工作一旦我关闭模拟器并重建数据就消失了。我对此做了一些研究,发现了许多像这样的StackOverflow线程:
Entities saved to RKManagedObjectStore's mainQueueManagedObjectContext disappear on next build
在大多数情况下提供相同的解决方案 - 从save:切换到saveToPersistentStore: - 但不幸的是,即使使用此更改,我的保存仍然不会持续存在。我非常感谢任何帮助!
My App Delegate,基本上可以在基本的RestKit教程中找到:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSError *error = nil;
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"appName" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
// Initialize the Core Data stack
[managedObjectStore createPersistentStoreCoordinator];
NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
NSAssert(persistentStore, @"Failed to add persistent store: %@", error);
[managedObjectStore createManagedObjectContexts];
// Set the default store shared instance
[RKManagedObjectStore setDefaultStore:managedObjectStore];
// Override point for customization after application launch.
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
LoginVC *controller = (LoginVC *) navigationController.topViewController;
controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
return YES;
}
实际创建NSManagedObject对象的代码:
- (void) theSaveButtonOnTheAddDayVCWasTapped:(AddDaytimeVC *)controller{
[controller.navigationController popViewControllerAnimated:YES];
if([[controller.activityDescriptionTextField text] length] > 0){
NSManagedObjectContext *context = self.managedObjectContext;
Daytime *daytime = [context insertNewObjectForEntityForName:@"Daytime"];
daytime.activityDescription = [controller.activityDescriptionTextField text];
daytime.thoughts = [controller.thoughtsTextField text];
NSError *error;
if (![context saveToPersistentStore:&error]) {
NSLog(@"Error saving context");
}
[self reloadArrayWithCurrentViewActivityType:@"Daytime"];
[[self tableView] reloadData];
}
}
我通过标准:
在视图之间传递mainQueueManagedObjectContext@property (nonatomic, strong) NSManagedObjectContext* managedObjectContext;
在prepareForSegue中设置。我最近还做了一个小模型更改(添加了一个字段)并重新生成了模型对象,如果这可能有任何帮助...我很遗憾不完全确定它是否在此之前按预期工作。
答案 0 :(得分:0)
如果有人好奇,事实证明这是相当直接的。整个“内存”持久性存储让我从一开始就被抛弃但我不认为教程会使用这样一个临时的例子......我错了: - )。
在我的app appate中添加:
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Fringe.sqlite"];
将persistentStore更改为:
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:[self optionsForSqliteStore] error:&error];
并补充说:
- (id)optionsForSqliteStore {
return @{
NSInferMappingModelAutomaticallyOption: @YES,
NSMigratePersistentStoresAutomaticallyOption: @YES
};
}
配置SQLite存储。一切都很好