【问题标题】:RestKit -Problems with object mapping of "advanced data object"?RestKit - “高级数据对象”的对象映射问题?
【发布时间】:2011-12-09 07:12:01
【问题描述】:

我在映射中遇到问题:latest_update 字段 JSON 数据。

从我的网络服务接收这个 JSON 数据:

{"Places":[ 
    {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a", 
     "timestamp": "2011-10-02 23:24:42" 
    }, 
    {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x", 
     "timestamp": "2011-10-02 23:24:42" 
    }, 
    {"latest_update":"2011-10-13 12:16:17"}] 

}

以及我用于托管映射的代码片段:

 //Place mapping 
    if (!self.placeManagedObject){ 
        self.placeManagedObject = [RKManagedObjectMapping 
mappingForClass:[Place class]]; 
        self.placeManagedObject.primaryKeyAttribute = @"UUID"; 
        self.placeManagedObject.rootKeyPath = @"places"; 
        [self.placeManagedObject mapKeyPath:@"place_ID" 
toAttribute:@"UUID"]; 
        [self.placeManagedObject mapKeyPath:@"timestamp" 
toAttribute:@"timestamp"]; 
        [self.placeManagedObject mapRelationship:@"PlaceInformation" 
withMapping:self.placeInfoManagedObject]; 
        // Register mapping with the provider - means it will look for 
places in the JSON input 
        [objectManager.mappingProvider 
setMapping:self.placeManagedObject forKeyPath:@"places"]; 
    } 
    //latestDBUpdate timestamp mapping 
    if (!self.latestDBUpdateManagedObject){ 
        self.latestDBUpdateManagedObject = [RKManagedObjectMapping 
mappingForClass:[LatestDBUpdate class]]; 
        self.latestDBUpdateManagedObject.primaryKeyAttribute = 
@"latest_update"; 
        self.latestDBUpdateManagedObject.rootKeyPath = @"places"; 
        [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update" 
toAttribute:@"latest_update"]; 
        // Register mapping with the provider - means it will look for 
places in the JSON input 
        [objectManager.mappingProvider 
setMapping:self.latestDBUpdateManagedObject 
forKeyPath:@"latest_update"]; 
    } 

RestKit 将正确映射 Place 对象,即: {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a", “时间戳”:“2011-10-02 23:24:42” } ... 放置物体, 但 latest_update 未映射到 LatestDBUpdate 类 对象,我无法让它工作。

我希望有人知道它是如何完成的,因为几个小时 搜索和尝试并没有让我更接近解决方案。 谢谢托马斯

【问题讨论】:

    标签: objective-c ios restkit object-object-mapping


    【解决方案1】:

    所以我终于想通了。 基本上是一些小的调整,以及代表我的几个错误:-/

    所以我将我的 web 服务的 JSON 输出更改为这种格式:

    {"Places":[ 
    {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a", 
     "timestamp": "2011-10-02 23:24:42" 
    }, 
    {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x", 
     "timestamp": "2011-10-02 23:24:42" 
    }], 
    "Timestamps":[
    {"latest_update":"2011-10-13 12:16:17"}]
    

    我在映射 og Timestamps -> latest_update 时出错,所以我修复了这个问题:

    //latestDBUpdate timestamp mapping
    if (!self.latestDBUpdateManagedObject){
        self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]];
        self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update";
        self.latestDBUpdateManagedObject.rootKeyPath = @"Timestamps";
        [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update" toAttribute:@"latest_update"];
    
        // Register mapping with the provider - means it will look for the keyword Timestamps in the JSON input
        [self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"];
    

    然后我的 get 方法出现了一个重大错误。基本上我告诉 RestKit 响应应该只由我的 Place 映射映射,因此不检查 JSON 输入中的所有其他数据是否有其他关键字。 只是为了展示我使用的两种 GET 方法的区别。第一个将仅使用指定的映射方案进行映射,第二个将检查所有已注册的映射方案:

    GET 方法号 1:(在上述情况下不起作用!)

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl objectMapping:self.placeManagedObject delegate:self];
    

    GET 方法 2:(这里检查了所有映射方案,并映射了两个 Place 对象,以及我的 LatestDBUpdate 对象!)

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl delegate:self];
    

    希望有一天有人会需要这个:-D

    托马斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 2017-12-15
      相关资源
      最近更新 更多