RestKit、NSFetchedResultsController 和删除孤立对象
Posted
技术标签:
【中文标题】RestKit、NSFetchedResultsController 和删除孤立对象【英文标题】:RestKit, NSFetchedResultsController, and deleting orphaned objects 【发布时间】:2013-12-23 15:37:02 【问题描述】:我使用 NSFetchedResultsController 设置了 RestKit 并与 API 和核心数据正常工作,以显示信息。一切都运行良好,除了在服务器上删除本地对象后删除它们。我尝试按照 RestKit 文档使用 RKObjectManager 删除孤立对象,但它似乎不起作用。这就是我所拥有的,任何见解都会很棒:
对象映射
+ (RKMapping *)locationMapping
//Create object map
//Same mapping as object mapping, but to NSManagedObjects
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Location" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
//Map the identical json / object properties
[mapping addAttributeMappingsFromArray:@[@"name", @"city", @"zip", @"recipient", @"street", @"state"]];
//Map the non-identical properties (left side = json, right side = NSObject)
[mapping addAttributeMappingsFromDictionary:@
@"id": @"locationID",
@"user_id":@"userID"
];
[mapping setIdentificationAttributes:@[@"locationID"]];
[mapping addRelationshipMappingWithSourceKeyPath:@"user" mapping:[self userMapping]];
[mapping addConnectionForRelationship:@"user" connectedBy:@"userID"];
RKObjectManager *rkObjectManager = [RKObjectManager sharedManager];
//Delete any orphaned objects
[rkObjectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL)
RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/api/v1/:userID/locations"];
NSDictionary *argsDict = nil;
if ([pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict])
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Location"];
return fetchRequest;
return nil;
];
return mapping;
注意 - 运行映射或进行 API 调用时,addFetchRequestBlock 未执行
拉取数据
+(void)loadUserLocations
//Don't try to load if there is no logged in user
if(![self getCurrentUser])return;
//Create an index of successful status codes
NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
//Pull location mapping from Mapping Provider
RKMapping *mapping = [MappingProvider locationMapping];
//Determine how rest kit will deal with the response. Specific to API calls
RKResponseDescriptor *rkResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
method:RKRequestMethodGET
pathPattern:@"/api/v1/users/:userID/locations"
keyPath:nil
statusCodes:statusCodeSet];
//Create the URL & response
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/users/%@/locations", [Statics baseURL], [self getCurrentUser].userID]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//Create the operation. Pass through AFHttp (more options) or NSURL
//When the request hits the path pattern (in this case, /locations),
//it will use the mapping to create the appropriate items
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[rkResponseDescriptor]];
//Set the cache
operation.managedObjectCache = [RKManagedObjectStore defaultStore].managedObjectCache;
operation.managedObjectContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
failure:^(RKObjectRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
NSLog(@"Response: %@", operation.HTTPRequestOperation.responseString);
];
[operation start];
设置 NSFetchedResultsController
-(NSFetchedResultsController*)fetchedResultsController
if (_fetchedResultsController != nil)
return _fetchedResultsController;
NSFetchRequest *fetchRequest = [[RKManagedObjectStore defaultStore].managedObjectModel fetchRequestFromTemplateWithName:@"userLocations" substitutionVariables:@@"USERID":[User getCurrentUser].userID];
//Sort the request
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"locationID" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
//Create a new controller and set it to the VC
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.mainMOC sectionNameKeyPath:nil
cacheName:@"Root"];
NSLog(@"%@",fetchRequest);
self.fetchedResultsController.delegate = self;
return self.fetchedResultsController;
这可能是缓存问题吗?还是我设置不当?
【问题讨论】:
【参考方案1】:您的 fetch 请求块中的路径模式 /api/v1/:userID/locations
与您的 GET 请求不匹配。
但您的主要问题是,要使用获取请求块,对象管理器需要运行请求。因为它是您显式创建请求和操作并执行。您应该完全创建和配置对象管理器并使用它来发出请求(这也应该是您编写的更少代码)。
【讨论】:
第一个问题:Doh!第二个问题:这很有意义。我并没有真正从示例中明确收集到这一点。谢谢!以上是关于RestKit、NSFetchedResultsController 和删除孤立对象的主要内容,如果未能解决你的问题,请参考以下文章
RestKit 0.20:restkit 对象映射使属性映射加倍
将“import <RestKit/RestKit.h>”添加到 AppDelegate.m 的问题
Restkit 0.20.x cocoapods 安装问题 - 编译但找不到 RestKit 的导入