Question

i'm using restkit to map a json with core data

i need to call a routine every time the user launch the app.

if the server has updated data to send, i need to download them, truncate my table and insert the data in the table, if the server sends me nothing i don't have to do nothing.

obviously i want to truncate my current data only when i'm sure that new data has been downloaded, not before

how can I achieve this?

this is my code:

NSURL *endpoint = [NSURL URLWithString:kBaseURL];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:endpoint];

[objectManager.HTTPClient setAuthorizationHeaderWithToken:@"username:my-token-12345"];
objectManager.managedObjectStore = [RKManagedObjectStore defaultStore];

[RKObjectManager setSharedManager:objectManager];

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Medic" inManagedObjectStore:objectManager.managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
                                                    @"identifier":          @"identifier",
                                                    @"name":                @"name",
                                                    @"surname":             @"surname",
                                                    @"personalAddress":     @"personalAddress",
                                                    @"hospital":            @"hospital",
                                                    @"hospitalAddress":     @"hospitalAddress",
                                                    @"oldDigitalAgreement": @"oldDigitalAgreement",
                                                    @"oldPaperAgreement":   @"oldPaperAgreement"}];
entityMapping.identificationAttributes = @[@"identifier"];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor
                                            responseDescriptorWithMapping:entityMapping
                                            method:RKRequestMethodAny
                                            pathPattern:nil
                                            keyPath:nil
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];


[[RKObjectManager sharedManager] getObjectsAtPath:kregistryURL
                                       parameters:nil
                                          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                              // done
                                          }
                                          failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                              UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"attention", nil) message:NSLocalizedString(@"communication.genericerror.title", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
                                              [alertView show];
                                          }
 ];

solution after Wain answer

[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher* pathMatcher = [RKPathMatcher pathMatcherWithPattern:kregistryURL];

    NSDictionary *dic = nil;
    if ([pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:YES parsedArguments:&dic]) {

        NSFetchRequest *fetchRequest = [NSFetchRequest new];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Medic"
                                                  inManagedObjectContext: [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext];
        fetchRequest.entity = entity;
        return fetchRequest;
    } else
        return nil;
}];
Was it helpful?

Solution

Your problem is

if the server sends me nothing, i don't have to do _any_thing

and I'm guessing the server still sends a 200 response rather than a special status code (it should be 304 ‘Not Modified’). Because if you didn't have that requirement you could use a deletion fetch request block (which is the correct solution).

As it stands, you will likely have to do the deletion yourself by fetching the existing items from the data store and iterating over them, checking if the item is in the mapping results provided to you in the success block and, if not, deleting it from the context...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top