【问题标题】:Multi root with a GET request带有 GET 请求的多根
【发布时间】:2014-03-20 14:24:37
【问题描述】:

我正在寻找一种使用以下 JSON 映射 2 个不同数组的方法

"establishments": [
    {
        "id": 1,
        "name": "Boudin Sourdough Bakery and Cafe",
        "address": "1 Stockton St",
        "zip_code": "94108",
        "city": "San Francisco",
        "country": "us",
        "phone_number": "",
        "position": {
            "latitude": 37.78590500000001,
            "longitude": -122.406289
        },
        "distance_from": 13.75783097391759
    }
],
"places": [
    {
        "id": 3,
        "name": "Private Place",
        "position": {
            "latitude": 37.78583400000001,
            "longitude": -122.406417
        },
        "is_private": false,
        "distance_from": 0
    }
]

我有 2 个不同的映射。一个用于建立,另一个用于场所。我在documentation 上找到了一些帮助,但它仅适用于 PUT 或 POST 请求。

目前,我有以下要求

[session.objectManager getObjectsAtPathForRouteNamed:APICallSearchSearchPlaceRouteName object:nil parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSArray * resultPlaces=mappingResult.array;

    [delegate APICallDidSearch:resultPlaces];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSError* myError=[self catchCustomErrorInRKError:error setDomain:@"Search"];
    if (myError.code == NSURLErrorCancelled) return;

    [delegate APICallDidFailSearch:myError];
}];

映射和映射描述符

RKEntityMapping *establishmentMapping = [APICallEstablishment RKGetEstablishmentMappingForManagedObjectStore:self.appDelegate.managedObjectStore];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:establishmentMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:APICallSearchSearchPlacePattern
                                                                                       keyPath:@"establishments"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[session.objectManager addResponseDescriptor:responseDescriptor];

我试图用这个例子来适应我的情况(就在我上面写的请求之前)

RKManagedObjectRequestOperation *operation = [session.objectManager appropriateObjectRequestOperationWithObject:establishmentMapping method:RKRequestMethodGET path:@"/whatever" parameters:nil];
operation.targetObject = nil;
//Does it still exist ? (XCode says an error)
//operation.targetObjectID = nil;
[session.objectManager enqueueObjectRequestOperation:operation];

提前感谢您的帮助。

【问题讨论】:

  • 你的映射和描述符是什么?它以什么方式仅适用于 put 和 post?
  • 我刚刚将此信息添加到主帖中。我不知道如何使用operation 添加多根。使用我当前的代码,我只能给出一个映射的结构 (establishmentMapping)。
  • 我想我找到了一些可行的方法,但我只能提取一个数组(我已经为每个映射复制了操作)。您认为可以根据映射的元素类型提取 2 个不同的数组吗?

标签: ios objective-c json restkit


【解决方案1】:

使用session.objectManager getObjectsAtPathForRouteNamed... 是一个不错的选择(比尝试直接使用RKManagedObjectRequestOperation 更好)。

您需要创建另一个响应描述符,例如 responseDescriptor,但用于 places 键路径和映射。

【讨论】:

  • 这正是我所做的。 :-) 我想我会自己管理数组的拆分。似乎容易多了。感谢您的帮助(再次)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 2021-12-19
  • 1970-01-01
  • 2019-06-08
相关资源
最近更新 更多