RestKit 数据同步后本地删除问题 [英] RestKit data synchronization post local-deletion issue

查看:51
本文介绍了RestKit 数据同步后本地删除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 RestKit 用于应用的后端.一切正常,但是我遇到了在本地删除后获取未更改数据的问题.

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.

这是匹配的路径:

  [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 响应缺少本地对象之一时,删除会正确传播.

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

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

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.

即使服务器返回正确的 JSON,RKMappingResult 也是空的:

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

我可以做些什么来解决这个问题?这似乎是某种缓存问题.然而,即使在本地删除后立即运行 [RKManagedObjectStore.defaultStore resetPersistentStores:nil] 函数似乎也不能解决这个问题.

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.

感谢您的帮助,

达伦

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

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

标识属性默认为card_id,所以不手动指定.

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

谢谢!

推荐答案

实际上,您必须从persistentstore 中获取数据,而不是从当前创建的托管上下文中获取数据.我的意思是 [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext

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

这篇关于RestKit 数据同步后本地删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆