Ios RestKit-将数据库与本地JSON文件同步

Ios RestKit-将数据库与本地JSON文件同步,ios,json,core-data,synchronization,restkit-0.20,Ios,Json,Core Data,Synchronization,Restkit 0.20,我提供了一个示例JSON文件(出于测试目的,我没有提前访问web服务)。加载文件并转换为NSDictionary后,如何使用该字典并同步数据库?我读过的所有教程和示例都使用web服务 我已经为所有对象创建了映射,并应用了它们的关系 例如: + (RKEntityMapping *) mapTableInManagedObjectStore:(RKManagedObjectStore *)managedObjectStore { RKEntityMapping *tableMapping

我提供了一个示例JSON文件(出于测试目的,我没有提前访问web服务)。加载文件并转换为
NSDictionary
后,如何使用该字典并同步数据库?我读过的所有教程和示例都使用web服务

我已经为所有对象创建了映射,并应用了它们的关系

例如:

+ (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

是否尝试使用文件URL?它过去是有用的,但我似乎记得有什么东西把它弄坏了。否则,只需将文件托管在某个位置,然后更改用于稍后访问它的URL即可。