【发布时间】:2017-06-04 05:44:55
【问题描述】:
是否可以设置类似全局规则的东西
RKbjectManager 在成功映射时删除孤立对象。
我目前正在使用RKPathMatcher 删除特定路径模式的孤儿,但似乎我错过了一些东西。
【问题讨论】:
标签: ios swift swift3 restkit ios10
是否可以设置类似全局规则的东西
RKbjectManager 在成功映射时删除孤立对象。
我目前正在使用RKPathMatcher 删除特定路径模式的孤儿,但似乎我错过了一些东西。
【问题讨论】:
标签: ios swift swift3 restkit ios10
阅读RKManagedObjectRequestOperation 中的“Fetch Request Blocks”,它们是预先配置的 NSFetchRequests,用于从本地存储中返回给定类型的所有对象,以便 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;
}
那么您只需要一个获取请求块来匹配您的任何实体。
【讨论】: