RestKit – 与本地JSON文件同步数据库

我提供了一个示例JSON文件(用于testing目的,我没有提前访问Web服务)。 在加载文件和转换为NSDictionary ,我怎样才能使用该字典和同步我的数据库? 我读过的所有教程和示例都使用了Web服务

我已经创build了所有对象的映射并应用它们的关系。

一个例子:

 + (RKEntityMapping *) mapTableInManagedObjectStore:(RKManagedObjectStore *)managedObjectStore { RKEntityMapping *tableMapping = [RKEntityMapping mappingForEntityForName:@"Table" inManagedObjectStore:managedObjectStore]; tableMapping.identificationAttributes = @[@"tableID"]; [tableMapping addAttributeMappingsFromDictionary:@{ @"ID":@"tableID", @"TableNumber":@"tableNumber", @"NumberOfChairs":@"numberOfChairs"}]; return tableMapping; } 

以下是我的做法和运作情况:

  // read file NSString *myJSON = [[NSString alloc] initWithContentsOfFile:contentPath encoding:NSUTF8StringEncoding error:NULL]; NSString* MIMEType = @"application/json"; NSError* parseError; NSData *data = [myJSON dataUsingEncoding:NSUTF8StringEncoding]; id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&parseError]; if (parsedData == nil && parseError) { NSLog(@"Cannot parse data: %@", parseError); } //convert NSData to NSDictionary NSError *errorJson=nil; NSDictionary* responseDict = [NSJSONSerialization JSONObjectWithData:lookServerResponseData options:kNilOptions error:&errorJson]; NSDictionary *tableDic = responseDict; //perform mapping RKManagedObjectStore *managedObjectStore = [HAObjectManager sharedManager].managedObjectStore; Table *table = [[Table findAll] firstObject]; RKManagedObjectMappingOperationDataSource *mappingDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext cache:managedObjectStore.managedObjectCache]; RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:tableDic destinationObject:table mapping:[MappingProvider mapTablebjectStore:managedObjectStore]]; mappingOperation.dataSource = mappingDataSource; NSError *error = nil; [mappingOperation performMapping:&error]; 

RKMappingOperation作业是将值从源对象映射到目标对象。
现在表格属性用tableDic值更新。 (表是一种NSManagedObject