2013-02-26 82 views
4

我在RestKit和CoreData上挣扎了一番,特别是因为RestKit 0.20中有那么少的例子和文档。Restkit 0.20:POST与外键的CoreData关系

我有一个(托管)对象SongAlbum具有多对一的关系。以下代码可以发布JSON,但不包含平铺的格式,服务器除外。

// Defined elsewhere 
Album *theAlbum; 
RKObjectManager *objMan = [self objectManager]; 

// Response Mapping 
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Song class]]; 
[responseMapping addAttributeMappingsFromDictionary:@{ @"song": @"songID" }]; 
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); 
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping 
                        pathPattern:@"/api/song" 
                         keyPath:nil 
                        statusCodes:statusCodes]; 

// Request Mapping 
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; 
RKEntityMapping *albumRelationshipMapping = [RKEntityMapping mappingForEntityForName:@"Album" inManagedObjectStore:[objMan managedObjectStore]]; 
[albumRelationshipMapping addAttributeMappingsFromDictionary:@{@"id": @"albumID", }]; 
[requestMapping addAttributeMappingsFromDictionary:@{ @"title": @"title", @"length": @"length" }]; 
[requestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"album" 
                       toKeyPath:@"album" 
                      withMapping:albumRelationshipMapping]]; 
requestMapping = [requestMapping inverseMapping]; 
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[Song class] rootKeyPath:nil]; 

[objMan addRequestDescriptor:requestDescriptor]; 
[objMan addResponseDescriptor:responseDescriptor]; 

// Create a new temporary song object 
Song *song = [NSEntityDescription insertNewObjectForEntityForName:@"Song" 
               inManagedObjectContext:[[objMan managedObjectStore] mainQueueManagedObjectContext]]; 
song.title = @"Some Title"; 
song.length = 123; 
song.album = theAlbum; 

// Post operation 
objMan.requestSerializationMIMEType = RKMIMETypeJSON; 
RKManagedObjectRequestOperation *operation = [objMan appropriateObjectRequestOperationWithObject:song 
                          method:RKRequestMethodPOST 
                          path:@"/api/song" 
                         parameters:nil]; 
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
    // Success 
} failure:^(RKObjectRequestOperation *operation, NSError *error) { 
    // Failure 
}]; 
[objMan enqueueObjectRequestOperation:operation]; 

此代码将发布JSON体是这样的:

{"title":"Some Title","length":"123","album":{"id":"6e32ae476815f365"}}

但是,服务器需要一个JSON体是这样的:

{"title":"Some Title","length":"123","album":"6e32ae476815f365"}

即关系album应该是一个拼合外键而不是嵌套对象。但是当我试图改变这样的albumRelationshipMapping

[albumRelationshipMapping setIdentificationAttributes:@[ @"albumID" ]]; 
[albumRelationshipMapping addAttributeMappingToKeyOfRepresentationFromAttribute:@"albumID"]; 

它引发一个异常。 (NSInvalidArgumentException', reason: '*** -[NSProxy doesNotRecognizeSelector:allKeys] called!'

有人知道我在做什么错吗?还是有代码在那里,这可以引导我在正确的方向吗?

对不起,如果这个问题已经在其他地方回答。我搜索了所有的stackoverflow和谷歌组,但无法找到一个特定的解决方案(RestKit 0.20,CoreData,与FK的关系)。

感谢,德克

回答

5

在这种情况下,我想你可以简单地使用点符号直接得到ALBUMID(无需使用RKRelationshipMapping)

尝试更新你的代码是这样的:

​​
+0

谢谢!我想的太复杂了。 – 2013-02-28 11:52:29

+0

我正在遵循相同的解决方案,但得到'终止应用程序由于未捕获的异常'NSUnknownKeyException',原因:'[<专辑0x145e1680> valueForUndefinedKey:]:此类不是关键值编码兼容的关键专辑。任何想法为什么会发生? – Zorayr 2013-10-22 07:13:28

+0

@Zorayr在我的例子中,专辑是您映射到的类的属性。如果找不到解决方案,也许试着用一些代码来问一个新问题。 – 2013-10-22 10:45:32