So I switched to using RestKit 0.2 and CoreData and I’ve been having a lot of trouble trying to get the mappings correct… I don’t understand why. The JSON Response of my server is like this:
{
"meta":
{
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 2
},
"objects":
[{
"creation_date": "2012-10-15T20:16:47",
"description": "",
"id": 1,
"last_modified":
"2012-10-15T20:16:47",
"order": 1,
"other_names": "",
"primary_name": "Mixing",
"production_line": "/api/rest/productionlines/1/",
"resource_uri": "/api/rest/cells/1/"
},
{
"creation_date": "2012-10-15T20:16:47",
"description": "",
"id": 2,
"last_modified": "2012-10-15T20:16:47",
"order": 2, "other_names": "",
"primary_name": "Packaging",
"production_line": "/api/rest/productionlines/1/",
"resource_uri": "/api/rest/cells/2/"
}]
}
Then in XCode I have:
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
RKEntityMapping *cellMapping = [RKEntityMapping mappingForEntityForName:@"Cell" inManagedObjectStore:managedObjectStore];
cellMapping.primaryKeyAttribute = @"identifier";
[cellMapping addAttributeMappingsFromDictionary:@{
@"id": @"identifier",
@"primary_name": @"primaryName",
}];
RKResponseDescriptor *responseCell = [RKResponseDescriptor responseDescriptorWithMapping:cellMapping
pathPattern:@"/api/rest/cells/?format=json"
keyPath:@"objects"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:@[responseCell, responseUser, responseCompany]];
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"AppDB.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"SeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
My request is:
[[RKObjectManager sharedManager] getObjectsAtPath:@"/api/rest/cells/?format=json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load complete: Table should refresh...");
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastUpdatedAt"];
[[NSUserDefaults standardUserDefaults] synchronize];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Load failed with error: %@", error);
}];
And I always get the following error:
**Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Unable to find any mappings for the given content" UserInfo=0x1102d500 {DetailedErrors=(), NSLocalizedDescription=Unable to find any mappings for the given content, keyPath=null}**
Thanks a lot!
UPDATE: I have added cellMapping.forceCollectionMapping = YES;
but still no luck :(!
UPDATE #2: Following Blake’s advice, I tried to change the path and it worked! I did /api/rest/cells/ instead of /api/rest/cells/?format=json and my server returned me everything and the mapping was successful!
Now the only problem I get is the following error:
2012-11-21 14:48:49.414 App[3125:617] W restkit.object_mapping:RKMapperOperation.m:176 Collection mapping forced but mappable objects is of type ‘__NSCFArray’ rather than NSDictionary
It sounds like the response descriptor is failing to match for the URL requested. Two ideas:
The next thing I would try is using the debugger to step through the matching. Within
RKObjectManagera list of candidate response mapping is built by theRKFilteredArrayOfResponseDescriptorsMatchingPathfunction. If your expected response mapping is not being returned in there, then the request path to path pattern is failing to evaluate.If things look good there, the next place a mismatch could occur is in
RKResponseMapperOperationwithin thebuildResponseMappingsDictionarymethod. This method evaluates the response against each response descriptor. If the response is failing to match against your response descriptor, then you’ll get unexpected results here.The final place to check is within RKResponseMapperOperation. This takes the mappings from matched descriptors and applies them. The RKMapperOperation
mainmethod should contain the deserialized response you expect and the appropriate mappings on themappingsDictionaryproperty.