gpt4 book ai didi

ios - 休息套件 0.20 : POST CoreData relationship with Foreign-Key

转载 作者:可可西里 更新时间:2023-11-01 05:36:57 25 4
gpt4 key购买 nike

我在使用 RestKit 和 CoreData 时遇到了一些困难,尤其是因为 RestKit 0.20 的示例和文档太少了。

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

// 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',原因:'*** -[NSProxy doesNotRecognizeSelector:allKeys] 已调用!')

有人知道我在这里做错了什么吗?或者是否有示例代码可以引导我朝着正确的方向前进?

很抱歉,如果这个问题已经在其他地方得到了回答。我搜索了所有 stackoverflow 和谷歌群组,但找不到适合我的案例的特定解决方案(RestKit 0.20、CoreData、与 FK 的关系)。

谢谢,德克

最佳答案

在这种情况下,我认为你可以简单地使用点符号直接获取albumID(不需要使用RKRelationshipMapping)

尝试以这种方式更新您的代码:

// 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 addAttributeMappingsFromDictionary:@{ @"title": @"title", @"length": @"length", @"album" : @"album.albumID" }];
//[requestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"album"
// toKeyPath:@"album"
// withMapping:albumRelationshipMapping]];

关于ios - 休息套件 0.20 : POST CoreData relationship with Foreign-Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15089405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com