1

我有以下映射:

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

2 回答 2

3

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

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

因此,将您的模式设置为匹配到/api/rest/productionparameters. 一旦你得到一个匹配,你想queryURL. 然后,您可以使用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;
}];
于 2013-08-27T15:36:23.663 回答
1

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

RKPathMatchermatchesPath:会将tokenizeQueryStrings:YES参数插入到传递的字典中。

于 2014-06-12T02:57:16.257 回答