RestKit 删除孤立对象规则

RestKit delete orphaned objects rule

是否可以设置类似全局规则的东西 RKbjectManager 删除映射成功时的孤立对象。

我目前正在使用 RKPathMatcher 删除特定路径模式的孤儿,但我似乎遗漏了一些东西。

阅读 RKManagedObjectRequestOperation 中的 "Fetch Request Blocks",它们是预先配置的 NSFetchRequests return 本地存储中给定类型的所有对象,以便 RestKit 可以找到没有的孤儿服务器响应中的时间更长。

他们的示例块仅检查机场航站楼:

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:@"http://restkit.org"];
[manager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/airports/:airport_id/terminals.json"];

    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
    NSString *airportID;
    if (match) {
        airportID = [argsDict objectForKey:@"airport_id"];
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Terminal"];
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"airportID = %@", @([airportID integerValue])]; // NOTE: Coerced from string to number
        fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES] ];
        return fetchRequest;
    }

    return nil;
}];

但是如果您的 URL 更通用,您可以将 pathMatcher 设置为匹配任何模式:

RKPathMatcher *entityMatcher = [RKPathMatcher pathMatcherWithPattern:@"/tables/:entityName"];

BOOL entityMatch = (pathAndQueryString != nil) && [entityMatcher matchesPath:pathAndQueryString tokenizeQueryStrings:YES parsedArguments:&argsDict];
if (entityMatch) {
  NSString *entityName = argsDict[@"entityName"];

  NSFetchRequest *fetchRequest;
  if (entityName) {
    fetchRequest = [NSFetchRequest fetchRequestWithEntityName:entityName];
    fetchRequest.includesSubentities = NO;
  }

  return fetchRequest;
}

那么您只需要一个获取请求块来匹配您的任何实体。