Seeking recommendations for best RestKit/CoreData mapping and JSON structure for shallow routes

孤人 提交于 2019-12-10 18:55:29

问题


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.


回答1:


Faults are a Core Data concept. RestKit is involved with creating objects in the data store, but it is the act of fetching them from the data store which faults them. It sounds more like what you're interested in is having RestKit create 'stub' objects in the data store (objects with the correct id and relationship but no detailed attributes set) so they can be filled in later when required.

Your current mappings and JSON for /A/1.json are fine for creating a stub instance of A and stubs of the connected Bs.

B/2.json would only create a stub B (it wouldn't do anything with A as the mapping has no relationship information attached). It's the mapping that is at 'fault', not the JSON. Add an RKRelationshipMapping to the mapping for B and the stub would be created and connection made.

That said, you would not usually be making a request for B which required 'back-stubbing' of A, because you would usually need to have requested A in order to get B.

Finally, you don't need different mappings for stubs and detail. Just have 1 mapping which contains all the detail and id mappings and RestKit will do everything that it can based on the received JSON (so if the JSON only contains ids you will just get stub objects).




回答2:


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.



来源:https://stackoverflow.com/questions/21538170/seeking-recommendations-for-best-restkit-coredata-mapping-and-json-structure-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!