2
votes

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

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

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.

1
also my dictionary argsDict is empty. can any body tell me why is this happening ? - Umair
Your json response is an Array. - Akhilrajtr

1 Answers

4
votes

You can't do this:

listID = [argsDict objectForKey:@"list_id"];
NSLog(@"The listID is %@",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.