ios - 使用 Restkit 从 JSON 数组映射关系

标签 ios json swift restkit restkit-0.20

我会尽力对这个问题进行描述......

场景

假设我有一个 NSManagedObject“用户”

@class Premise;

@interface User : NSManagedObject

@property (nonatomic, retain) NSNumber * identifier;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *premises;
@end

@interface User (CoreDataGeneratedAccessors)

- (void)addPremisesObject:(Premise *)value;
- (void)removePremisesObject:(Premise *)value;
- (void)addPremises:(NSSet *)values;
- (void)removePremises:(NSSet *)values;

@end

我还有一个 NSManagedObject '前提'

@class User;

@interface Premise : NSManagedObject

@property (nonatomic, retain) NSNumber * identifier;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) User *user;

@end

基于此,我正在创建一个关系路由,以将“Premise”的 JSON 数组映射到“User”对象上的“premises”属性。

路线如下:

 let getPremisesRoute = RKRoute(relationshipName: "premises", 
                                objectClass: User.self, 
                                pathPattern: "user/:identifier/premises", 
                                method: .GET)

这是 JSON 响应 (/user/1/premises):

[
    {
        "id": 35,
        "name": "Icaraí"
    },
    {
        "id": 32,
        "name": "Remanso"
    }
]

这是响应描述符:

  let getPremisesResponseDescriptor = RKResponseDescriptor(mapping: premiseMapping, method: .GET, pathPattern: "user/:identifier/premises", keyPath: nil, statusCodes: RKStatusCodeIndexSetForClass(.Successful))

这是“用户”和“前提”各自的映射

let userMapping = RKEntityMapping(forEntityForName: "User", inManagedObjectStore: moc)
userMapping.addAttributeMappingsFromDictionary(["id":"identifier", "name":"name"])      
userMapping.identificationAttributes = ["identifier"]
userMapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: nil, toKeyPath: "premises", withMapping: premiseMapping))


let premiseMapping = RKEntityMapping(forEntityForName: "Premise", inManagedObjectStore: moc)
premiseMapping.addAttributeMappingsFromDictionary(["id":"identifier", "name":"name"])
premiseMapping.identificationAttributes = ["identifier"]

现在解决我的问题

显然,Restkit 在映射过程中变得有点困惑。这是请求后的数据库:

用户表: enter image description here

前提表:

enter image description here

请注意,实体之间并未创建关系。

现在,如果我将响应描述符的映射从前提更改为用户映射,数据库将更改为:

用户表:

enter image description here

前提表:

enter image description here

我对发生的事情感到非常困惑,并且尝试了很多解决方案但没有成功。

JSON 响应是否不符合模式或者我做错了什么? JSON 响应似乎采用常见模式,具有 nil 键路径。

最佳答案

您正在错误地处理映射,或者至少您的映射对于您正在做的事情来说是错误的。考虑响应是一个用户,但只是用户的前提,而不是像现在一样将其视为简单的前提列表。然后映射到用户并插入场所。像这样的东西:

RKResponseDescriptor(mapping: userMapping, method: .GET, pathPattern: "user/:identifier/premises", keyPath: nil, statusCodes: RKStatusCodeIndexSetForClass(.Successful))

这是“用户”和“前提”各自的映射

let userMapping = RKEntityMapping(forEntityForName: "User", inManagedObjectStore: moc)
userMapping.addAttributeMappingsFromDictionary(["@metadata.routing.parameters.idEntities":"identifier"])      
userMapping.identificationAttributes = ["identifier"]
userMapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: nil, toKeyPath: "premises", withMapping: premiseMapping))


let premiseMapping = RKEntityMapping(forEntityForName: "Premise", inManagedObjectStore: moc)
premiseMapping.addAttributeMappingsFromDictionary(["id":"identifier", "name":"name"])
premiseMapping.identificationAttributes = ["identifier"]

您在响应中没有用户名,因此无法映射它,并且用户 ID 实际上在请求中,因此您需要使用元数据来提取它。

关于ios - 使用 Restkit 从 JSON 数组映射关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987723/

相关文章:

json - -- swift : async + JSON + completion + DispatchGroup

iOS 9 读取文件权限

iphone - 在未来的应用程序版本中维护核心数据

ios - "Tell Friend"允许选择多个联系人的示例

json - R data.frame 到带有子节点/分层结构的 JSON

javascript - 验证包含日期的 Javascript 对象

ios - nsfetchedResultsController - 合并数据时出错

ios - 无需重新初始化即可访问 View Controller

python - 使用python从ustream api解码json

ios - 在图像上绘制内容后,我的 ImageView contentmode 停止工作