寻找浅层路由的最佳RestKit / CoreData映射和JSON结构的建议 [英] Seeking recommendations for best RestKit/CoreData mapping and JSON structure for shallow routes

查看:94
本文介绍了寻找浅层路由的最佳RestKit / CoreData映射和JSON结构的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个由(Rails)REST API(支持浅路由)支持的CoreData iOS应用程序。因为图中有很多对象,我希望REST GET不要包含大量的嵌套结果,相反,只包含RestKit用来建立(故障)关系的引用。然后我将使用浅路由根据需要请求个体(或多个)对象。



假设我有一对多(< - & >)数据模型例如A→B,我有:

  RKEntityMapping * a_mapping = [RKEntityMapping mappingForEntityForName: @AinManagedObjectStore:managedObjectStore]; 
[a_mapping addAttributeMappingsFromDictionary:@ {@a_id:@aId,...}];
a_mapping.identificationAttributes = @ [@aId];

RKEntityMapping * b_mapping = [RKEntityMapping mappedForEntityForName:@BinManagedObjectStore:managedObjectStore];
[b_mapping addAttributeMappingsFromDictionary:@ {@b_id:@bId,...}];
b_mapping.identificationAttributes = @ [@bId];

[a_mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@bstoKeyPath:@bswithMapping:b_mapping]];

我有这些路线:

  NSIndexSet * statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); 

RKResponseDescriptor * a_ResponseDescriptor;
a_ResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:a_mapping method:RKRequestMethodGET pathPattern:@/ A /:aIdkeyPath:@AstatusCodes:statusCodes];

RKResponseDescriptor * b_ResponseDescriptor;
NSIndexSet * statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
b_ResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:b_mapping method:RKRequestMethodGET pathPattern:@/ B /:bIdkeyPath:@BstatusCodes:statusCodes];

[[RKObjectManager sharedManager] addResponseDescriptor:a_ResponseDescriptor];
[[RKObjectManager sharedManager] addResponseDescriptor:b_ResponseDescriptor];

我有几个相关问题:




  • 当返回'A'记录时,我应如何构造JSON,以便RestKit将为任何相关的B对象实例化存根?

  • $ b $类似地,如果我想请求一组B对象(没有A对象的先验知识),如何在返回一个'B'记录时结构化JSON,以便RestKit将实例化拥有' A对象?



    b
    $ b

    目前,我有一个方向工作(A - > B),但我似乎不知道如何让逆向工作。特别的,/A/1.json返回如下:

      {a:{a_id:1, bs:[{b_id:2},{b_id:3}]}} 

    And B / 2.json返回:

      {b:{b_id:2,a_id:1 }} 

    我应该使用类似:

      {b:{b_id:2,a:{a_id:1}}} 
    / pre>

    解决方案

    要解决Wain所说的问题,解决方案是:




    • 在为A和B对象发送JSON时,建立双向RKRelationshipMappings

    • 使用带有嵌套映射的映射。例如, {b:{b_id:2,a:{a_id:1}}}



    如果你使用iOS模拟器,请务必将HTTP响应头设置为不缓存内容。这使我多次,因为我的代码使用过时的数据。


    I'm developing a CoreData iOS app that's backed by a (Rails) REST API (that supports shallow routes). Because there are a lot of objects in the graph, I'd like the REST GETs to not to include a lot of nested results and, instead, just contain references that RestKit uses to establish (faulted) relationships. I'll then use shallow routes to request the individual (or groups of) objects as needed.

    Assuming I have a one-to-many (<-->>) data model such as A <-->> B, I have:

    RKEntityMapping *a_mapping = [RKEntityMapping mappingForEntityForName:@"A" inManagedObjectStore:managedObjectStore];
    [a_mapping addAttributeMappingsFromDictionary:@{@"a_id" : @"aId", ...}];
    a_mapping.identificationAttributes = @[@"aId"];
    
    RKEntityMapping *b_mapping = [RKEntityMapping mappingForEntityForName:@"B" inManagedObjectStore:managedObjectStore];
    [b_mapping addAttributeMappingsFromDictionary:@{@"b_id" : @"bId", ...}];
    b_mapping.identificationAttributes = @[@"bId"];
    
    [a_mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"bs" toKeyPath:@"bs" withMapping:b_mapping]];
    

    I have these routes:

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
    
    RKResponseDescriptor *a_ResponseDescriptor;
    a_ResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:a_mapping method:RKRequestMethodGET pathPattern:@"/A/:aId" keyPath:@"A" statusCodes:statusCodes];
    
    RKResponseDescriptor *b_ResponseDescriptor;
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
    b_ResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:b_mapping method:RKRequestMethodGET pathPattern:@"/B/:bId" keyPath:@"B" statusCodes:statusCodes];
    
    [[RKObjectManager sharedManager] addResponseDescriptor:a_ResponseDescriptor];
    [[RKObjectManager sharedManager] addResponseDescriptor:b_ResponseDescriptor];
    

    I have a couple of related questions:

    • How should I structure the JSON when returning an 'A' record so that RestKit will instantiate stubs for any related 'B' objects?

    • Similarly, if I want to request a bunch of B objects (without prior knowledge of A objects) how do I structure the JSON when returning a 'B' record so that RestKit will instantiate stubs for the owning 'A' object?

    • What additional setup/code do I need with RestKit?

    Currently, I have one direction working (A --> B), but I can't seem to figure out how to get the reverse to work. In particular, /A/1.json returns something like:

    {"a": {"a_id":1, "bs":[{"b_id": 2}, {"b_id": 3}]}}
    

    And B/2.json returns:

    {"b": {"b_id":2, "a_id": 1}}
    

    Should I instead be using something like:

    {"b": {"b_id":2, "a": {"a_id": 1}}} 
    

    ? Any help/advice would be appreciated.

    解决方案

    To build off of what Wain said, the solution is to:

    • establish bidirectional RKRelationshipMappings
    • use mappings with nested mappings when sending JSON for both A and B objects. Ex. {"b": {"b_id":2, "a": {"a_id": 1}}}

    Also, if you're working with the iOS simulator, be sure to set your HTTP response headers to not cache things. This nailed me a bunch of times since my code was using stale data.

    这篇关于寻找浅层路由的最佳RestKit / CoreData映射和JSON结构的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆