2015-10-14 64 views
0

我有途径RestKit postObject使用参数不更新发送对象

[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> 

UPDATE

不是,我得到的对象,而不是一个数组喜欢这里

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

+0

请添加解决方案作为一个答案,并接受它;-) – Wain

回答

0

我发现为什么它不能正常工作。由于targetObject不工作

问题,不仅匹配responseDescriptors,但所有的WTF设置此功能

if (RKDoesArrayOfResponseDescriptorsContainMappingForClass(self.responseDescriptors, [object class])) operation.targetObject = object; 
  1. 这是工作,所有的描述?

  2. 并且它返回NO作为对象。

该对象的映射是RKDynamicMapping。

发现问题

以下问题:不要使用setObjectMappingForRepresentationBlock

因为U将得到

po [mapping objectMappings] 
<__NSArray0 0x7fcdd8d0ad30>(

) 

而且RKMappingGraphVisitor的功能将无法正常工作。

SOLUTION 1

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

更好的解决方案

使用类似

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

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

    return mapping; 
}