RestKit - 使用本地JSON文件同步数据库(RestKit - Sync Data Base with local JSON file)

IPhone IT屋
问 题

I have provided with a sample JSON file (for testing purpose I haven't early access to the web service). after loading the file and converting to NSDictionary how can I use that dictionary and sync my data base? all the tutorials and samples I've read use web service

I have created my mapping for all of the objects and applied their relationship.

an example:

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

    return tableMapping;
}
解决方案

Here is how I did and works well:

 // 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 job is to map values from source object to destination object.
now table attributes are updated with tableDic values. (table is kind of NSManagedObject)

本文地址:IT屋 » RestKit - Sync Data Base with local JSON file

问 题

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



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



一个例子:



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

return tableMapping;
}

解决方案

工作良好:



  //读取文件
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错误:& parseError];
if(parsedData == nil& parseError){
NSLog(@“无法解析数据:%@”,parseError);
}

//将NSData转换为NSDictionary
NSError * errorJson = nil;
NSDictionary * responseDict = [NSJSONSerialization JSONObjectWithData:lookServerResponseData options:kNilOptions error:& errorJson];
NSDictionary * tableDic = responseDict;

//执行映射
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作业是将值从源对象映射到目标对象。

now表属性使用tableDic值进行更新。 (表是 NSManagedObject )


本文地址:IT屋 » RestKit - 使用本地JSON文件同步数据库