我将RestKit用于我的其余实现。我从服务器获取数据,并接收到数据,然后调用saveToPersistentStore
也成功,但是当我关闭Internet并再次加载该页面时,它在存储中找不到任何数据。
这是初始化流程-
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Storage" 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];
我对每一个使用mainQueue-
controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
这是执行提取的功能:
[CMLRKSharedManager getNewsForUser:userProfile
urlParams:dict
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[self.tableView reloadData];
NSLog(@"Fetched object count %lu %@ %@", (unsigned long)self.fetchedResultsController.fetchedObjects.count , self.fetchedResultsController, self.fetchedResultsController.managedObjectContext);
NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSError *executeError = nil;
if(![managedObjCtx saveToPersistentStore:&executeError]) {
NSLog(@"Failed to save to data store");
}
else
NSLog(@"SAVE SUCCESSFUL");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
[self.tableView reloadData];
}];
}
输出:
2014-04-02 22:24:01.547获取的对象计数4
2014-04-02 22:24:01.555保存成功
现在,我杀死了该应用程序,关闭了wifi,然后再次启动该应用程序。该表现在为空白,我看到
fetchResultsController
在控制台日志中看到0个项目4月2日22:24:36:在numberOfRows中获取的对象计数0
我不确定为什么...
@wain我将代码更新如下-
NSError *error = nil;
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Cover" 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);
//
而是添加了sqlite数据库,但现在该应用程序崩溃了
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (! persistentStore) {
RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}
最佳答案
根据您的描述,问题在于您只有一个内存存储:
[managedObjectStore addInMemoryPersistentStore:&error];
因此什么都不会保存到磁盘。您应该更改此设置,以便使用SQLite存储。这会将数据保存到磁盘,并且在大多数使用情况下通常效果更好。
关于ios - RestKit saveToPersistentStore不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22828692/