-->

RestKit: how to remove core data entries to keep t

2020-07-11 05:34发布

问题:

I'm using the RestKit RKObjectManager to get data from my server and store in core data (see my other post)

I want to configure the deletion behavior of the old entries left in the database.

I saw there's a deletionPredicate property in the RKEntityMapping class but I understand that it is used only when the service actually returns the objects to be deleted flagged as 'to-be-deleted'. (am I right?)

In my case when some objects have to be deleted, they're just NOT returned by the server and I'd like to make my client app understand that this means it should remove them.

Is that possible? And if so, how?

EDIT:

OK I got a look at that link and I added this fetch request block to my RKObjectManager :

[[RKObjectManager sharedManager] addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {

    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/path_to_ressource"];

    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];

    if (match) {

        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
        fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"entityId" ascending:YES] ];
        return fetchRequest;
    }

    return nil;
}];

I kept the sortDescriptor but what is exactly its purpose here?

回答1:

You want to look at the section "Fetch Request Blocks and Deleting Orphaned Objects" on this page. It requires you to be using an RKObjectManager (which you say you are) and describes the way that you tell RestKit how to find content in the data store that should be deleted (and it checks and doesn't delete things that it just received from the server).