我拼命试图使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];
}
最佳答案
如果您仍在寻找答案,那么我今天在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];
关于ios - 如何为核心数据持久性配置RestKit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18382254/