RestKit data synchronization post local-deletion i

2019-08-28 03:01发布

问题:

I'm using RestKit for an app's backend. Everything is working well, however I'm running into an issue with fetching unaltered data post local deletion.

This is the path matching:

  [self.objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"cards"];
    if([pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:nil])
      return [NSFetchRequest fetchRequestWithEntityName:@"Card"];
    return nil;
  }];

Deletions are properly propagating when the API response is missing one of the local objects.

However, when local deletions are performed like:

  for(NSManagedObject *entity in cards) {
    [RKManagedObjectStore.defaultStore.mainQueueManagedObjectContext deleteObject:card];

If the /cards fetch request is performed again, the cards are not updated if the objects that were just deleted in CoreData have not changed. This continues to be the case until the app is relaunched or a new object is included in the API response.

The RKMappingResult is empty even though the server is returning the proper JSON:

  2013-07-03 17:58:00.732 Thanx[76837:c07] I restkit.network:RKHTTPRequestOperation.m:185 GET 'http://api.thanx-web.dev/v2/cards' (200 OK) [0.1086 s]
  2013-07-03 17:58:00.734 Thanx[76837:c07] <RKMappingResult: 0xac9b080, results={
      "<null>" =     (
      );
  }>
  2013-07-03 17:58:00.734 Thanx[76837:c07] <RKManagedObjectRequestOperation: 0xbb32250, state: Successful, isCancelled=NO, request: <NSMutableURLRequest http://api.thanx-web.dev/v2/cards>, response: <NSHTTPURLResponse: 0xbb42060 statusCode=200 MIMEType=application/json length=201>>

Is there anything that I could do to fix this issue? It seems to be some sort of caching issue. However, even running the [RKManagedObjectStore.defaultStore resetPersistentStores:nil] function immediately after local deletion doesn't seem to solve this.

Thanks for the help,

Darren

EDIT - Here are the applicable model mappings:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Card" inManagedObjectStore:self.objectManager.managedObjectStore];
// enable validations
mapping.performKeyValueValidation = YES;
// if validation fails, skip object rather than entire request
mapping.discardsInvalidObjectsOnInsert = YES;
// add json-to-model mappings
[mapping addAttributeMappingsFromArray:@"card_id", @"last4", @"name"];

The identification attribute defaults to card_id, so it is not manually specified.

Thanks!

回答1:

In fact, you have to fetch datas from the persistentstore and not the current created managed context. I mean [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyModel"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:NO];
fetchRequest.sortDescriptors = @[descriptor];

// Setup fetched results
NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                        managedObjectContext:[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];

// AND TO DELETE A MODEL :

[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext deleteObject:myobject];