6

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?

Community
  • 1
  • 1
Alexis
  • 16,629
  • 17
  • 62
  • 107

1 Answers1

5

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).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I modified the question, could you confirm it's that simple? and what about using a sort descriptor in this case? – Alexis Jun 19 '13 at 14:56
  • Don't add the / to the start of the pattern ("/path_to_ressource") as that will often cause it to not match the request you make. I don't believe the sort descriptor is required (I never use one) and can't see what benefit it would provide. – Wain Jun 19 '13 at 15:02