Search code examples
ioscore-datarestkitrestkit-0.20

RestKit Fetch Request Block (Deletion of orphaned objects)


I am using restkit. My coredata entities are arranged as shown in the screeshotCore Data Tables

I have added following fetch request block to delete orphaned objects

[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
 RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/api/lists/:phone_no"];

    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
    NSString *phone_no;
    if (match)
    {
        phone_no = [argsDict objectForKey:@"phone_no"];
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"List"];
        //we don't need request predicate here. We delete all local items which are not present in retruned response.

        return fetchRequest;
    }
    return nil;

 }];

It works fine for orphaned List and Task objects. For example if a task of some list gets deleted over the server, then on refreshing it also disappears form local store and hence the table-view.

But child of Task like 'Task Log', 'Task Comment' and 'Task Note' don't get deleted form local store if their server counterparts are deleted.

For example if task note of task is deleted from sever then on refreshing data the 'now' orphaned task comment still shows in the table.

I am using this GET Descriptor which returns all the relevant data in appropriate JSON Format

//Response Descriptor GET for List
RKResponseDescriptor *listResponseDescriptorGET = [RKResponseDescriptor responseDescriptorWithMapping:listEntityMapping
                                                                                               method:RKRequestMethodGET
                                                                                          pathPattern:@"/api/lists/:phone_no"
                                                                                              keyPath:nil
                                                                                          statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

What i am doing wrong? I use fetch results controllers everywhere which automatically updates views.

I can provide more information if required.


Solution

  • You aren't doing anything wrong as such, but your expectation is wrong.

    If tasks are deleted when lists are deleted that is because of a Core Data deletion rule, not because of RestKit. RestKit will only find List objects in the store to delete.

    You can configure deletion rules in Core Data to cascade the deletion from the task to its owned relationship endpoints too, but again, this isn't directly part of RestKit.