我正在拼命尝试将RestKit核心数据配置正确,但我必须做错事,因为当我加载对象时,它们被正确映射但从不持久。 我最终复制/粘贴了RKTwitterCoreData示例中的所有内容并获得了以下代码,但我的数据库中仍然没有得到任何内容:
- (void)configureRestKit {
//[[AFHTTPRequestOperationLogger sharedLogger] setLevel:AFLoggerLevelDebug];
//[[AFHTTPRequestOperationLogger sharedLogger] startLogging];
// Initialize RestKit
NSString *serverUrl = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFServerUrl"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:serverUrl]];
objectManager.HTTPClient = [CFHTTPClient sharedClient];
// Enable Activity Indicator Spinner
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
// Initialize managed object store
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
// Setup our object mappings
/**
Mapping by entity. Here we are configuring a mapping by targetting a Core Data entity with a specific
name. This allows us to map back Twitter user objects directly onto NSManagedObject instances --
there is no backing model class!
*/
RKEntityMapping *eventMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:managedObjectStore];
eventMapping.identificationAttributes = @[@"eventId"];
[eventMapping addAttributeMappingsFromArray:@[@"eventId", @"title", @"startDate", @"endDate", @"city", @"country"]];
[...]
// Update date format
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"d/M/yyyy"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[RKObjectMapping setDefaultDateFormatters:[NSArray arrayWithObject:dateFormatter]];
// Register our mappings with the provider
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"events" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Event class] pathPattern:@"event" method:RKRequestMethodPOST]];
[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Event class] pathPattern:@"event/:eventId" method:RKRequestMethodGET]];
/**
Complete Core Data stack initialization
*/
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Chapter4.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{NSInferMappingModelAutomaticallyOption : @YES, NSMigratePersistentStoresAutomaticallyOption :@YES} error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
}
答案 0 :(得分:3)
如果您仍在寻找答案,我今天在RestKit 0.22.0中遇到了同样的问题,并通过手动创建NSPersistentStoreCoordinator
并使用它来初始化RKManagedObjectStore
来获得持久性而不是NSManagedObjectContext
。
尝试此操作(为清晰起见,省略了错误检查):
// Model
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
// Manually create a NSPersistentStoreCoordinator
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
// Add your persistent store to the coordinator
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Chapter4.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:nil
error:nil];
// Init the RKManagedObjectStore with the coordinator
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:persistentStoreCoordinator];
// Let it create the NSManagedObjectContexts
[managedObjectStore createManagedObjectContexts];
objectManager.managedObjectStore = managedObjectStore;
然后,假设您正在mainQueueManagedObjectContext
工作,请将其保存为:
[objectManager.managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:nil];
答案 1 :(得分:1)
由于RestKit依赖于CoreData,因此需要保存ManagedObjectContext以保留数据。我不确定你的版本是什么,但我保存了这样的数据:
[[RKObjectManager sharedManager].objectStore.managedObjectContextForCurrentThread save:nil];
将其放在objectLoaderDidFinishLoading:(RKObjectLoader*)objectLoader
内,您的对象应该保存。