RKObjectManager:在添加之前检查requestDescriptors

时间:2014-04-19 08:49:10

标签: ios objective-c restkit

我试图搜索我的RKObjectManager,如果我已经注册的说明已经注册。 由于requestDescriptors,我发现有一个描述符列表,但我无法将它们与我的进行比较。

我绝对需要检查这些描述符,因为如果它已经存在(这使我的应用程序崩溃)我不能添加另一个描述符:

[objectManager addRequestDescriptor:requestDescriptor];

我尝试使用

进行简单的迭代
BOOL toAdd = YES;
for (RKRequestDescriptor *desc in objectManager.requestDescriptors) {
  if ([desc isEqualToRequestDescriptor:requestDescriptor]) {
     toAdd = NO;
   }
}
if (toAdd) {
  [objectManager addRequestDescriptor:requestDescriptor];
}

但我可以在调试器上看到:

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>"
)>>

所以我对等式的测试是错误的,我的变量toAdd仍然是真的。

我遇到同样的问题:

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

请参阅调试器的比较:

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>"
)>

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

RKRequestDescriptor类有一个比较两个描述符的方法。

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

所以你可以迭代请求描述符数组并检查它是否已经在那里:

例如:

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