RestKit NSFetchRequest用于删除孤立对象的块 [英] RestKit NSFetchRequest Block for deletion of orphaned objects

查看:97
本文介绍了RestKit NSFetchRequest用于删除孤立对象的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Rest api hosted over my .com domain which on receiving request like this www.mydomain.com/api/lists will return the json formatted data as shown below

[
    {"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块以使用此代码检查孤立对象

now i am adding a NSFetchRequest Block in my RKObjectManger to check for orphaned objects using this code

[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。请指导我这里出了什么问题。

but my listID = [argsDict objectForKey:@"list_id"]; returns nil, despite the fact the returned json from web has "list_id" in it. Please guide me what is going wrong here.

推荐答案

你不能这样做:

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

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

because there is no list_id parameter in the path pattern /api/lists. So, if this data is missing, your predicate will always be checking for the wrong listID.

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

But, you don't need any of that. Assuming that the request returns all of the objects from the server - the only time you should be using fetch blocks - then you don't need a predicate. Just create a fetch request for the entity and return it without any predicate.

这篇关于RestKit NSFetchRequest用于删除孤立对象的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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