RestKit NSFetchRequest用于删除孤立对象的块

时间:2014-01-20 11:08:52

标签: ios objective-c json rest restkit

我的.com域名托管了一个Rest api,收到请求后,www.mydomain.com / api / list将返回json格式的数据,如下所示

[
    {"list_id":"1","listName":"List Name 1"},
    {"list_id":"2","listName":"List Name 2"},
    {"list_id":"5","listName":"List Name 3"},
    {"list_id":"7","listName":"List Name 4"},
    {"list_id":"8","listName":"List Name 5"},
    {"list_id":"11","listName":"List Name 6"},
    {"list_id":"12","listName":"List Name 7"}
]

现在我在我的RKObjectManger中添加一个NSFetchRequest块,以使用此代码检查孤立对象

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

        NSDictionary *argsDict = nil;
        BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO     parsedArguments:&argsDict];
        NSString *listID;
        if (match)
        {
            listID = [argsDict objectForKey:@"list_id"];
            NSLog(@"The listID is %@",listID);
            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"List"
                                                      inManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
            [fetchRequest setEntity:entity];
            fetchRequest.predicate = [NSPredicate predicateWithFormat:@"listID = %@", @([listID integerValue])]; 
            return fetchRequest;
        }

        return nil;
    }];

但我的listID = [argsDict objectForKey:@"list_id"];返回nil,尽管从web返回的json中有“list_id”。请指导我这里出了什么问题。

1 个答案:

答案 0 :(得分:4)

你不能这样做:

listID = [argsDict objectForKey:@"list_id"];
NSLog(@"The listID is %@",listID);

因为路径模式list_id中没有/api/lists参数。因此,如果缺少此数据,您的谓词将始终检查错误的listID

但是,你不需要任何这些。假设请求从服务器返回所有对象 - 您应该使用获取块的唯一时间 - 那么您不需要谓词。只需为实体创建一个获取请求,并在没有任何谓词的情况下返回它。