RestKit: how to remove core data entries to keep the content in sync with the server?

旧街凉风 提交于 2019-12-21 13:35:30

问题


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



来源:https://stackoverflow.com/questions/17110445/restkit-how-to-remove-core-data-entries-to-keep-the-content-in-sync-with-the-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!