无法使RKPathMatcher * pathMatcher匹配删除孤立对象

时间:2013-08-27 02:47:31

标签: objective-c restkit restkit-0.20

我有以下映射:

RKResponseDescriptor *productionParametersPerEquipment = [RKResponseDescriptor responseDescriptorWithMapping:productionParameterMapping
                                                                                                 pathPattern:@"/api/rest/productionparameters/?equipment=:equipment_id"
                                                                                                     keyPath:@"objects"
                                                                                                 statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

然后我有:

[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/api/rest/productionparameters/?equipment=:equipment_id"];

    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
    NSString *productionParameterID;
    if (match) {
        productionParameterID = [argsDict objectForKey:@"id"];
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ProductionParameter"];
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"identifier = %@", @([productionParameterID integerValue])]; // NOTE: Coerced from string to number
        fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES] ];
        return fetchRequest;
    }

    return nil;
}];

但是,每当我拨打以下内容时,从不匹配,因此永远不会删除我的孤儿。有什么想法吗?

[[RKObjectManager sharedManager] getObjectsAtPath:[NSString stringWithFormat:@"/api/rest/productionparameters/?equipment=%@",_equipment.identifier] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)

2 个答案:

答案 0 :(得分:4)

模式匹配器无法与URL的查询字符串中的模式匹配。查询中的任何内容都需要单独处理。您应该对URL路径进行模式匹配,然后在模式匹配时查询和处理参数。

注意,此模式匹配限制也适用于删除处理范围之外。

因此,将模式设置为与/api/rest/productionparameters匹配。获得匹配后,您希望从query获取URL。然后,您可以使用componentsSeparatedByString:将查询参数拆分为键值对,然后处理每对,直到找到equipment并提取id

顺便提一下,因为您将模式参数设置为[argsDict objectForKey:@"id"],所以模式永远不会返回equipment_id


在iPad上键入以便检查,您可能还想在阵列计数上添加一些检查......:

[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/api/rest/productionparameters"];

    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
    NSString *productionParameterID;

    if (match) {

        NSArray queryItems = [[URL query] componentsSeparatedByString:@"&"];

        for (NSString *item in queryItems) {

             NSArray *keyValPair = [item componentsSeparatedByString:@"="];

             if ([keyValPair[0] isEqualToString:@"equipment"]) {
                 productionParameterID = keyValPair[1];
                 NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ProductionParameter"];
                 fetchRequest.predicate = [NSPredicate predicateWithFormat:@"identifier = %@", @([productionParameterID integerValue])]; // NOTE: Coerced from string to number
                 fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES] ];

                return fetchRequest;
            }
        }
    }

    return nil;
}];

答案 1 :(得分:1)

使用[URL relativeString]代替[URL relativePath],因为relativePath省略了查询字符串。

带有matchesPath:的RKPathMatcher tokenizeQueryStrings:YES会将参数插入到传递的字典中。