Search code examples
iosobjective-crestkit

RKObjectManager : check requestDescriptors before adding


I'm trying to search on my RKObjectManager, if a description that I'm building is already registered or not. I've found that there is a list of descriptors thanks to requestDescriptors but I can't compare them to mine.

I absolutely need to check those descriptors because I cannot add another one if it's already existing (that makes my app crash) with this code:

[objectManager addRequestDescriptor:requestDescriptor];

I tried a simple iteration with

BOOL toAdd = YES;
for (RKRequestDescriptor *desc in objectManager.requestDescriptors) {
  if ([desc isEqualToRequestDescriptor:requestDescriptor]) {
     toAdd = NO;
   }
}
if (toAdd) {
  [objectManager addRequestDescriptor:requestDescriptor];
}

But I can see that on my debugger :

Printing description of desc:
<RKRequestDescriptor: 0xd366950 method=(POST) objectClass=User rootKeyPath=(null) : <RKObjectMapping:0xd3664d0 objectClass=NSMutableDictionary propertyMappings=(
    "<RKAttributeMapping: 0xd366510 facebookToken => fb_token>"
)>>
Printing description of requestDescriptor:
<RKRequestDescriptor: 0x12847d00 method=(POST) objectClass=User rootKeyPath=(null) : <RKObjectMapping:0x1284c3d0 objectClass=NSMutableDictionary propertyMappings=(
    "<RKAttributeMapping: 0x12842860 facebookToken => fb_token>"
)>>

So my test about equality is false and my variable toAdd still true.

I have the same problem with:

if (desc.mapping == requestDescriptor.mapping) {
  toAdd = NO;
}

See the comparison from the debugger:

Printing description of $0:
<RKObjectMapping:0xd03ac50 objectClass=NSMutableDictionary propertyMappings=(
    "<RKAttributeMapping: 0xd066530 facebookToken => fb_token>"
)>
Printing description of $1:
<RKObjectMapping:0xd431d90 objectClass=NSMutableDictionary propertyMappings=(
    "<RKAttributeMapping: 0xd4520b0 facebookToken => fb_token>"
)>

Thank you for your help.


Solution

  • The RKRequestDescriptor class has a method to compare two descriptors.

    /**
      Returns `YES` if the receiver and the specified request descriptor are considered equivalent.
     */
    - (BOOL)isEqualToRequestDescriptor:(RKRequestDescriptor *)otherDescriptor;
    

    So you can iterate the request descriptors array and check if it's already in there:

    For example:

    for (RKRequestDescriptor *r in [[RKObjectManager sharedManager] requestDescriptors]) {
        if ([r isEqualToRequestDescriptor:otherRequestDescriptor]) {
            // do something
        }
    }