Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm assuming you can't change the data structure of your "image" JSON object, so that will limit RestKit's functionality as far as automatic relational mapping goes. However, you can still set up a relationship yourself. </p> <p>First, you need to add a field to your data model, <code>appId</code>.</p> <p>Then, set up your image mapping, like this:</p> <pre><code>RKEntityMapping* imageMapping = [RKEntityMapping mappingForEntityForName:@"Image" inManagedObjectStore:objectManager.managedObjectStore]; [imageMapping addAttributeMappingsFromDictionary:@{ @"imgId" : @"imageId", @"iPath" : @"clientPath", @"iType" : @"type", @"sPath" : @"serverPath", @"appId" : @"appId" }]; imageMapping.identificationAttributes = @[ @"imageId" ]; RKResponseDescriptor *imageResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:imageMapping pathPattern:nil keyPath:@"image" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [objectManager addResponseDescriptor:imageResponseDescriptor]; </code></pre> <p>After this, your mapping should be done. To fill your local database with your objects, make this call, <code>getObjectsAtPath</code>:</p> <pre><code>[[RKObjectManager sharedManager] getObjectsAtPath:@"/yourWebServiceURL" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { // success block NSLog(@"Success!"); } failure:^(RKObjectRequestOperation *operation, NSError *error) { //fail block }]; </code></pre> <p>This should load all of your objects into CoreData. In another place in your app, maybe even in the success block of the above call, you'll need to connect your Image objects to your Application objects. You'll need fetch requests and a <code>for</code> loop. Just assign every <code>Image</code>'s "application" attribute to be the <code>Application</code> object which has the same <code>appId</code>, like this:</p> <pre><code>myImage.application = fetchedApplicationWithID; </code></pre> <p>You will then be able to look up the relationship both ways, with <code>[myImage application]</code> and with <code>[myApplication images]</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload