RestKit数据同步发布本地删除问题

时间:2013-07-06 23:57:01

标签: ios restkit restkit-0.20

我正在使用RestKit作为应用程序的后端。 一切都运行良好,但是我遇到了在本地删除后获取未经更改的数据的问题。

这是路径匹配:

  [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;
  }];

当API响应缺少一个本地对象时,删除正在传播。

然而,当执行本地删除时,如:

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

如果再次执行/ cards fetch请求,如果刚刚在CoreData中删除的对象没有更改,则不会更新卡。在应用程序重新启动或API响应中包含新对象之前,情况仍然如此。

即使服务器返回正确的JSON,RKMappingResult仍为空:

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

我有什么办法可以解决这个问题吗?这似乎是某种缓存问题。但是,即使在本地删除后立即运行[RKManagedObjectStore.defaultStore resetPersistentStores:nil]功能似乎也无法解决此问题。

感谢您的帮助,

的Darren

编辑 - 以下是适用的模型映射:

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"];

标识属性默认为card_id,因此不会手动指定。

谢谢!

1 个答案:

答案 0 :(得分:0)

实际上,您必须从persistentstore中获取数据,而不是从当前创建的托管上下文中获取数据。我的意思是[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];