gpt4 book ai didi

ios - RestKit 将未嵌套关系映射到拥有对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:40 24 4
gpt4 key购买 nike

我正在尝试将照片的 GET 响应与它们的所有权相关联 User使用 RestKit 在核心数据中创建对象(即能够在请求后调用 user.photos 并自动映射它们)。但是,我不明白如何将其设置为自动执行,而不是必须使用 [user setPhotos:...]明确来自 RKMappingResult

有没有办法使用映射和响应描述符自动完成此操作?

路由/响应描述符:

RKRoute *userPhotos = [RKRoute routeWithRelationshipName:@"photos" objectClass:[User class] pathPattern:@"/api/v1/photos" method:RKRequestMethodGET];
[[[[APIClient objectManager] router] routeSet] addRoute:userPhotos];

RKResponseDescriptor *photosDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:self.photoMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:kJsonPhotos
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[[APICacheStore sharedStore] objectManager] addResponseDescriptorsFromArray:@[photosDescriptor]];

代码:

+ (void)getPhotosForUser:(User *)user success:(void (^)())success failure:(void (^)(NSString *errorMsg))failure {
NSAssert(user, @"User must not be nil when we attempt to fetch their photos.");
[[APIClient objectManager] getObjectsAtPathForRelationship:@"photos" ofObject:user parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[user setPhotos:[NSSet setWithArray:[mappingResult array]]];
success();
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
failure(error.localizedDescription);
}];
}

原始 JSON:

{
"meta": {
"total": 2,
"offset": 0,
"limit": 10
},
"photos": [
{
"id": "d8e1ca59-9f50-4963-8e7e-0b0c8de4bed6",
"name": "Mary at the mall",
"updated_at": "2013-12-11T10:16:41.000Z",
"completed_at": null,
"created_at": "2013-12-11T10:16:41.000Z",
"image": "images/is9912zh/image.jpeg"
},
{
"id": "1c75273a-7675-43a9-9b62-3d4236439ec6",
"name": "John at the park",
"updated_at": "2013-12-11T10:16:41.000Z",
"completed_at": null,
"created_at": "2013-12-11T10:16:41.000Z",
"image": "images/aj92j127/image.jpeg"
}
]
}

最佳答案

与您似乎拥有的信息以及您发送请求的方式无关。

如果路径模式是 @"/api/v1/photos/##USER_ID##" 那么路由将为外键映射提供足够的信息。如果返回的 JSON 具有用户 ID,则将为外键映射提供足够的信息。

从技术上讲,该操作具有填充关系所需的所有信息,但 RestKit 不会(当前)尝试这样做。您可以考虑实现它并提交拉取请求。

关于ios - RestKit 将未嵌套关系映射到拥有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20546370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com