简体   繁体   中英

RestKit: two separate feeds, two different object types. One object manager?

I'm working with RestKit for the first time and am pretty happy, unfortunately the current project has thrown me for a loop.

First, I have two feeds. One for news and one for photos...

News feed...

[
    {"id":1234, "title":"Top story"},
    {"id":1235, "title":"Next story"},
    {"id":1236, "title":"Final story"}
]

Photos feed...

[
    {"id":1919, "url":"president.jpg"},
    {"id":1920, "url":"celebrity.jpg"},
    {"id":1921, "url":"sports.jpg"}
]

...in my Core Data model I have these entities created that match these attributes. I have also setup my mappings for RestKit...

RKManagedObjectMapping* newsMapping = [RKManagedObjectMapping mappingForEntityWithName:@"News"];
newsMapping.primaryKeyAttribute = @"id";

[newsMapping mapKeyPath:@"id" toAttribute:@"id"];
[newsMapping mapKeyPath:@"title" toAttribute:@"article_id"];

[objectManager.mappingProvider setMapping:newsMapping forKeyPath:@"News"];


RKManagedObjectMapping* photoMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Photo"];
photoMapping.primaryKeyAttribute = @"id";

[photoMapping mapKeyPath:@"id" toAttribute:@"id"];
[photoMapping mapKeyPath:@"url" toAttribute:@"url"];

[objectManager.mappingProvider setMapping:photoMapping forKeyPath:@"Photo"];

...in the above code I'm aware that the specified keyPaths aren't present in the feeds, but providing empty keyPaths just seemed to overwrite the mappings and RestKit seemed to ignore the absent keyPAths in the feeds. Next, in each of the view controllers where I'm displaying the objects I run some pretty similar code...

RKObjectManager* objectManager = [RKObjectManager sharedManager];
// objectManager.inferMappingsFromObjectTypes = YES;

[objectManager loadObjectsAtResourcePath:@"json/news" delegate:self block:^(RKObjectLoader* loader) {
    loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[News class]];
}];

...enough background. Together all this code loads zero news and zero photos. If I comment out the photos mappings, all of my news items populate and if I comment out the news mappings then my photos appear in the store.

RestKit can't match together each feed's content with its mapping. The keypaths specified in setMapping:forKeyPath: won't work because the feeds don't have anything specified. I thought maybe objectManager.inferMappingsFromObjectTypes = YES would have RestKit inspect what each feed had and choose the proper mappings, but it didn't. Also, I assumed objectMappingForClass: would automatically pickup the correct mappings, but no. Does anyone have any ideas how to make this work?

UPDATE: Actually, even though RestKit is reporting that no items had been loaded when the app runs the first time, if I stop and run again I now see all 20 news items and 20 photos loaded from cache. I confirmed this by running it once, seeing the message "Caching all 0 News objects to thread local storage" and then immediately opening the .sqlite file and seeing all 20 items as expected.

This comment: https://groups.google.com/d/msg/restkit/b0K0nCha8D0/VadnqDVB1-oJ sent me down the right path. I had a project with an existing Core Data stack and once I removed all related to code to that OTHER set of stores and contexts, etc. Everything worked a charm. What an awesome framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM