【发布时间】:2014-08-11 10:30:32
【问题描述】:
我正在开发一个 iPad 应用程序。对于我的应用程序,我使用 Restkit 对象映射从 WebApi 获取对象并保存在我的核心数据中。我有 2 个表名称 Class、Names、。每个表都有单独的字段示例
a) 姓名:- 名字,姓氏
b) 类:- A 类、B 类、D 类
Web Api 返回 JSON 数据,如 ---
{
"status": 200,
"statusMessage": "OK",
"message": "SUCCESS",
"data": {
"station": {
"Names": [
{
FirstName:"Richard"
Lastname:"Jecob"
},
{
FirstName:"David"
Lastname:"philiph"
}
],
"Class" : [
{
"ClassA": "Noramal",
"ClassB":"Average"
},
]
我为名称的一个实体映射示例编写了代码
NSArray *nameArray=[[NSArray alloc]initWithObjects:
@"firstName",
@"lastName",nil];
NSDictionary *nameDict=[[NSDictionary alloc]init];
NSArray *ideArray=[[NSArray alloc]initWithObjects:@"firstName",@"lastName" nil];
NSString *EntityName = @"Names";
NSString *pathPattern = [NSString stringWithFormat:@"/data"];
NSString *keyPath = [NSString stringWithFormat:@"data.station.Names"];
NSString *urlString = [NSString stringWithFormat:@"http://example.com/data"];
AppDelegate *ad = (TSAppDelegate *) [[UIApplication sharedApplication] delegate];
RKManagedObjectStore *managedObjectStore = ad.restKitManagedObjectStore;
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:EntityName inManagedObjectStore:managedObjectStore];
[mapping addAttributeMappingsFromArray:nameArray];
[mapping addAttributeMappingsFromDictionary:nameDict];
mapping.identificationAttributes = idArray;
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:pathPattern keyPath:keyPath statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = managedObjectStore.managedObjectCache;
// This property controls if the objects are persisted in the DB or not.
operation.savesToPersistentStore = YES;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"The public timeline Tweets: %@", [result array]);
} failure:nil];
[operation start];
当我提供一个实体时,它对我来说工作正常。我需要提供多个实体进行映射(名称,类)。我是 rest kit 映射的新手,有人帮助我重写上面的代码超过类和名称的一个对象映射
【问题讨论】:
标签: ios objective-c ipad core-data restkit