o15a3d4l11s2
o15a3d4l11s2

Reputation: 4059

RestKit - delete objects that are not returned

I am looking for a way to delete objects that exist locally in CoreData, but are no longer returned from the server's JSON response. I have looked at Fetch Request Blocks and Deleting Orphaned Objects, but in my case the objects does not have parent, so I think that it will not work for me. In my case I do not want to delete orphaned objects, because they never had a parent. Did I misunderstood the concept described in the example? Is there a way to do a similar fetch request that will gather all local records and compare them agains the server-returned ones, or I will have to do it manually after RestKit is done with the mapping?

EDIT:

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:@"http://example.com"];
[manager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/api/articles.json"];

    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
    if (match) {
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Article"];
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"articleID IN %@", ARTICLEIDS]
        fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES] ];
        return fetchRequest;
    }

    return nil;
}];

Is the following example correct? If so, the problem I face here is how to get the ARTICLEIDS from the server response?

Upvotes: 2

Views: 546

Answers (1)

Wain
Wain

Reputation: 119031

Yes, you misunderstood the meaning of orphan. In this case it doesn't mean that the object doesn't have a parent, it simply means that the object wasn't returned in the most recent request. This feature of RestKit does exactly what you're looking for - search for all objects in the data store, remove the ones just received, delete the remainder.

Upvotes: 1

Related Questions