I've been working with RestKit (0.9.3) to populate some Core Data in my app. The entity objects are created correctly, but the relationships between them are not created. From what I can tell, I have things setup correctly according to:
- https://github.com/RestKit/RestKit/wiki/Object-mapping
- RKRelationshipMappingExample in the Examples folder
So, here's a sample of the JSON that I'm getting back from the server:
{
"artist" : {
"artistId" : 123,
"artistName" : "The Artist",
"albums" : [
{
"albumPrice" : 12.99,
"albumId" : 456,
"trackCount" : 12,
"albumName" : "Album Title",
},
{
"albumPrice" : 10.99,
"albumId" : 789,
"trackCount" : 10,
"albumName" : "Another Album Title",
}
]
}
}
And I am using RestKit to populate the following two managed objects:
@interface Artist : NSManagedObject
@property (nonatomic, retain) NSNumber * artistId;
@property (nonatomic, retain) NSString * artistName;
@property (nonatomic, retain) NSSet *albums;
@end
@interface Artist (CoreDataGeneratedAccessors)
- (void)addAlbumsObject:(Album *)value;
- (void)removeAlbumsObject:(Album *)value;
- (void)addAlbums:(NSSet *)values;
- (void)removeAlbums:(NSSet *)values;
@end
@interface Album : NSManagedObject
@property (nonatomic, retain) NSNumber * albumId;
@property (nonatomic, retain) NSString * albumName;
@property (nonatomic, retain) NSNumber * albumPrice;
@property (nonatomic, retain) NSNumber * trackCount;
@property (nonatomic, retain) NSManagedObject *artist;
@end
I've setup the following configurations with RestKit, and the intent is for 3 Core Data items to be created: one artist and two albums. The artist entity has a "albums" relationship which should be a set of these two albums.
// Initialize our RestKit singleton
RKClient *client = [RKClient clientWithBaseURL:API_URL];
// Initialize for Object mapping
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:API_URL];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"AppName.sqlite"];
// Configure ALBUM mapping
RKManagedObjectMapping* albumMapping = [RKManagedObjectMapping mappingForClass:[Album class]];
albumMapping.primaryKeyAttribute = @"albumId";
[albumMapping mapKeyPath:@"albumId" toAttribute:@"albumId"];
[albumMapping mapKeyPath:@"albumName" toAttribute:@"albumName"];
[albumMapping mapKeyPath:@"albumPrice" toAttribute:@"albumPrice"];
[albumMapping mapKeyPath:@"trackCount" toAttribute:@"trackCount"];
// Configure ARTIST mapping
RKManagedObjectMapping* artistMapping = [RKManagedObjectMapping mappingForClass:[Artist class]];
artistMapping.primaryKeyAttribute = @"artistId";
[artistMapping mapKeyPath:@"artistId" toAttribute:@"artistId"];
[artistMapping mapKeyPath:@"artistName" toAttribute:@"artistName"];
[artistMapping mapKeyPath:@"albums" toRelationship:@"albums" withMapping:albumMapping];
// Register mappings with the provider
[[RKObjectManager sharedManager].mappingProvider setMapping:albumMapping forKeyPath:@"albums"];
[[RKObjectManager sharedManager].mappingProvider setMapping:artistMapping forKeyPath:@"artist"];
And then later in my program, I load the data:
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"/artist/123/albums" delegate:self];
After this is executed, my Core Data store is populated with the three objects (one artist and two albums), but the relationship between them is not created.
The result is that I can fetch this list of albums directly, or I can fetch the artist directly. But if I try to use: artist.albums
the result is empty.
Can anyone offer suggestions on what I could be doing wrong, or how to further troubleshoot this?
Thanks so much.
EDIT: Update 10/26
I just setup a small sample project with these same configurations and everything was loaded correctly (i.e. The artist entity was created with a relationship to it's albums). Sadly, this means that the issue is something in my larger project, rather than in what I outlined here earlier. With that said, I'll continue trudging though and updating, so if anyone has advice on how to troubleshoot this or where in particular to look, I would greatly appreciate it.
EDIT: Resolved
Embarrassingly, the problem was simply that I was using the wrong base url. I'll post full details (in case it can help anyone else) tomorrow.