简体   繁体   中英

RestKit postObject with params does not update send object

I have route

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

And response descriptor

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

Here route when post object

<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>";
    };
}

Before post a new Comment i create temp object

BBComment *wComm = [BBComment MR_createEntity];

and then

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

    }];

Firstly:

In RestKit operation create

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

it's empty

But i think it's not a key.

After response from server i got two different objects wComm and from responseResult

Why? I think what RestKit update given object instead creating a new one?

Here objects before and after 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>

I try save object before post (like a RestKit do if object is temp) - nothing changes.

I think maybe different contexts? But no

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

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

UPDATE

Not, i get object, not an array like here

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

I found why it's not work. Because targetObject not set in operation

Problem in this function

if (RKDoesArrayOfResponseDescriptorsContainMappingForClass(self.responseDescriptors, [object class])) operation.targetObject = object;
  1. It's work by ALL descriptors, not only for matching responseDescriptors, but ALL WTF?

  2. And it's return NO for object.

Mapping for this object is RKDynamicMapping.

PROBLEM FOUND

Here problem: not use setObjectMappingForRepresentationBlock

Because u will get

po [mapping objectMappings]
<__NSArray0 0x7fcdd8d0ad30>(

)

And function of RKMappingGraphVisitor will not work.

SOLUTION 1

Get RKOperation and set manually targetObject. But i think it's not correct.

BETTER SOLUTION

Use something like

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

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

    return mapping;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM