【问题标题】:RestKit postObject with params does not update send object带参数的 RestKit postObject 不更新发送对象
【发布时间】:2016-01-12 12:20:40
【问题描述】:

我有路线

  [RKRoute routeWithClass:[BBComment class] pathPattern:@"/comments" method:RKRequestMethodPOST]

和响应描述符

[RKResponseDescriptor responseDescriptorWithMapping:[BBComment apiMapping] method:RKRequestMethodPOST pathPattern:@"/comments" keyPath:nil statusCodes:successCodes]

发布对象时的路径

<RKClassRoute: 0x7fe2da8d65e0 objectClass=BBComment method=(POST) pathPattern=comment>
Printing description of routingMetadata:
{
    query =     {
        parameters =         {
            "post_id" = 3205;
            text = ds;
        };
    };
    routing =     {
        parameters =         {
        };
        route = "<RKClassRoute: 0x7fe2da8d65e0 objectClass=BBComment method=(POST) pathPattern=comment>";
    };
}

在发表新评论之前,我会创建临时对象

BBComment *wComm = [BBComment MR_createEntity];

然后

[[RKObjectManager sharedManager] postObject:wComm path:nil parameters:sendCommentData success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        BBLog(@"ret:%@", wComm);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {

    }];

首先:

在 RestKit 操作中创建

 NSSet *temporaryObjects = [[managedObjectContext insertedObjects] filteredSetUsingPredicate:temporaryObjectsPredicate];

它是空的

但我认为这不是钥匙。

在服务器响应后,我得到了两个不同的对象 wComm 和 responseResult

为什么?我认为什么 RestKit 更新给定对象而不是创建一个新对象?

POST 前后的对象

(lldb) po [wComm objectID]
0xd00000000050001c <x-coredata://C721341D-CAA2-4240-809B-D3F2D45032B5/BBComment/p20>

(lldb) po [mappingResult.firstObject objectID]
0xd00000000054001c <x-coredata://C721341D-CAA2-4240-809B-D3F2D45032B5/BBComment/p21>

我尝试在发布之前保存对象(如果对象是临时对象,就像 RestKit 所做的那样) - 没有任何变化。

我认为可能是不同的上下文?但是没有

(lldb) po wComm.managedObjectContext
<NSManagedObjectContext: 0x7fa442c42080>

(lldb) po [mappingResult.firstObject managedObjectContext]
<NSManagedObjectContext: 0x7fa442c42080>

更新

不是,我得到的是对象,不是像这里这样的数组

What is the relationship between the post object, in the RestKit postObject method, and the RKMappingResult it returns?

【问题讨论】:

  • 请添加解决方案作为答案并接受它;-)

标签: ios rest core-data restkit


【解决方案1】:

我找到了为什么它不起作用。因为操作中没有设置targetObject

这个函数的问题

if (RKDoesArrayOfResponseDescriptorsContainMappingForClass(self.responseDescriptors, [object class])) operation.targetObject = object;
  1. 它适用于所有描述符,不仅适用于匹配 responseDescriptors,还适用于所有 WTF?

  2. 对象返回 NO。

此对象的映射是 RKDynamicMapping。

发现问题

这里的问题:不使用setObjectMappingForRepresentationBlock

因为你会得到

po [mapping objectMappings]
<__NSArray0 0x7fcdd8d0ad30>(

)

并且RKMappingGraphVisitor的功能将不起作用。

解决方案 1

获取 RKOperation 并手动设置 targetObject。但我认为这是不正确的。

更好的解决方案

使用类似的东西

+ (RKDynamicMapping *)apiMapping {
    RKDynamicMapping *mapping = [RKDynamicMapping new];

    [mapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"type" expectedValue:BBMessageType.plain objectMapping:[self plainTypeMapping]]];

    return mapping;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多