【问题标题】:RestKit Collection - The Proper Key Path?RestKit Collection - 正确的密钥路径?
【发布时间】:2018-08-17 13:33:09
【问题描述】:

我的服务器有以下 JSON...

{"Devices": [
  {
    "uuid": "d9084134-d5f7-43a3-9613-7faa769a822a",
    "label": "",
    "location": "a13d45f4-5ce0-48e3-bc3f-4076bb007037",
    "capability": 0
  },
  {
    "uuid": "a4ee0d3f-4a6a-4c61-81bd-3dfa9ab19e85",
    "label": "",
    "location": "a13d45f4-5ce0-48e3-bc3f-4076bb007037",
    "capability": 3
  }
]}

我正在尝试正确映射它,但我正在努力解决它......我有以下映射设置......

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Device" inManagedObjectStore:managedObjectStore];

    [entityMapping setIdentificationAttributes:[NSArray arrayWithObject:@"uuid"]];

    [entityMapping addAttributeMappingsFromDictionary:@{
                                                        @"uuid":                        @"uuid",
                                                        @"label":                       @"label",
                                                        @"location":                    @"location",
                                                        @"capability":                  @"capability"
                                                        }];

    // GET

    RKRequestDescriptor *getRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[entityMapping inverseMapping] objectClass:[Device class] rootKeyPath:@"Device" method:RKRequestMethodGET];
    [[RKObjectManager sharedManager] addRequestDescriptor:getRequestDescriptor];

    [[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Device class] pathPattern:@"Devices" method:RKRequestMethodGET]];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:@"Devices" keyPath:@"Devices" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

我将我的 keyPath 设置为我认为可行的“设备”。但是 RestKit 不理解它。

我正在使用以下内容从我的服务器获取对象...

[[RKObjectManager sharedManager] getObjectsAtPath:@"Devices" parameters:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d", clientID], @"clientId", @"topic", @"forTopic", nil] success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    ...
}... // Abbreviated for brevity.

所以我相信我的路径“设备”对于“设备”的 pathPattern 是正确的。参数搞砸了吗?我之前有过参数,它总是可以工作,而无需在映射中指定任何内容。

我目前使用的是 RestKit 0.27.1。

更新 - 修复 JSON

我修复了 Mahendra GP 提到的 JSON。所以这现在是一个合适的数组。 RestKit 日志现在显示跟踪日志中的 JSON。但是它仍然无法识别它是哪个对象(设备)。

日志片段...

...and targetObject: (null)
2018-03-09 02:44:05.603734-0700 MyTestApp[31576:6868006] D restkit.object_mapping:RKMapperOperation.m:440 Finished performing object mapping. Results: (null)
2018-03-09 02:44:05.605334-0700 MyTestApp[31576:6868006] E restkit.network:RKObjectRequestOperation.m:172 GET 'http://[MY_IP]:8080/Devices?clientId=5199&forTopic=topic' (200 OK / 0 objects) [request=2.6488s mapping=0.0000s total=2.6633s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." 

【问题讨论】:

  • 检查您的 json 数据,它在“设备”下有重复的键“设备”...我们不允许!

标签: ios objective-c restkit restkit-0.20


【解决方案1】:

我终于通过侥幸的反复试验找到了我的问题。我无法在 SO 或 RestKit 的网站上找到任何特定的帖子。因此,我想我会在这里发布问题的简单解决方案。

我不断收到我的响应描述符都不匹配任何关键路径的错误。我认为这与我传递给 GET 的参数有关,因为它会显示在错误中尝试将其与设置响应描述符的设备路径进行比较。然而,事实证明这根本不是问题。

问题在于我的 RKResponseDescriptor 设置。我原来有...

  RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:@"Devices" keyPath:@"Devices" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

这里的解决方案是“pathPattern”。这需要设置为 nil

我已经使用 RestKit 很多年了。在 0.2x 版本之前。我记得 RestKit 能够识别一个实体,并且它只是通过复数来识别的。我终于想到,也许是因为我在 pathPattern 中指定它导致了问题。因此,一旦我最终将其设置为零,它终于开始工作了。我实际上认为将其设置为 nil 的原因就像将其设置为任何 pathPattern 的通配符,因为我的路径实际上有参数标记在它们上面。

但是在另一个用例中,我确实指定了 pathPattern,但它不是映射实体的复数,而是有所不同。因此,我有类似 DeviceForUser 之类的东西,而不是设备。所以在pathPattern中指定实体的复数时似乎有问题。特别是如果在 getObjectsAtPath 调用中使用参数。

所以为了清楚起见,上面的所有代码都是相同的。它只需要这个改变......

  RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"Devices" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

我希望这对可能遇到此问题的其他人有所帮助。

【讨论】:

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