【问题标题】:Object Mapping With Restkit to core data for MultipleTables使用 Restkit 将对象映射到 MultipleTables 的核心数据
【发布时间】: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


    【解决方案1】:

    首先,您需要创建 Core Data 模型和关系。我的建议是将它们划分为 3 个对象:Station、Name 和 Class。关系可以是:Station->>Name、Station->Class,其中分别为“->>”To-Many 和“->”To-One 关系。不要忘记创建反向关系。之后在 RestKit 中创建映射规则和关系。关系是使用 RKRelationshipMapping 对象创建的。

    【讨论】:

    • 您错过的重要信息是,如果您不想要第三个实体和关系,您需要具有不同关键路径和映射的多个响应描述符。
    • @razor28 感谢您的帮助。我创建了多响应描述符并解决了问题。
    猜你喜欢
    • 2014-09-03
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    相关资源
    最近更新 更多