无法让 RKPathMatcher *pathMatcher 匹配删除孤立对象 [英] Can't get RKPathMatcher *pathMatcher to match for deleting orphans objects

查看:43
本文介绍了无法让 RKPathMatcher *pathMatcher 匹配删除孤立对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下映射:

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

但是,每当我调用以下代码时,它从不匹配,因此我的孤儿永远不会被删除.有什么想法吗?

However, whenever I call the following, it never matches, hence my orphans are never deleted. Any ideas?

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

推荐答案

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

The pattern matcher can not match against patterns in the query string of the URL. Anything in the query needs to be handled separately. You should pattern match against the URL path and then query and process the parameters if / when the pattern matches.

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

Note, this pattern matching limitation applies outside of the scope of the deletion processing too.

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

So, set your pattern to match to /api/rest/productionparameters. Once you get a match you want to get the query from the URL. You can then use componentsSeparatedByString: to split the query parameters into key value pairs and then process each pair till you find equipment and extract the id.

顺便说一句,模式永远不会返回给你 [argsDict objectForKey:@"id"] 因为你将模式参数设置为 equipment_id.

Incidentally, the pattern would never have returned you [argsDict objectForKey:@"id"] anyway because you set the pattern parameter to equipment_id.

在 iPad 上输入所以检查,您可能还想添加一些数组计数检查...:

Typed on iPad so check, you probably want to add some checks on array counts too...:

[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;
}];

这篇关于无法让 RKPathMatcher *pathMatcher 匹配删除孤立对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆