0

我正在使用 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 获取请求,如果 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]在本地删除后立即运行该功能似乎也无法解决此问题。

谢谢您的帮助,

达伦

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

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,所以不用手动指定。

谢谢!

4

1 回答 1

0

事实上,您必须从持久存储中获取数据,而不是从当前创建的托管上下文中获取数据。我是说[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];
于 2013-07-07T01:46:31.567 回答