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

查看:675
本文介绍了RestKit - 使用本地JSON文件同步数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

一个例子:

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

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

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)

这篇关于RestKit - 使用本地JSON文件同步数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆