Ios RestKit、NSFetchedResultsController和删除孤立对象
我已使用NSFetchedResultsController设置RestKit,并使用API和核心数据正常工作,以显示信息。除了在服务器上删除本地对象后删除这些对象之外,所有这些都工作正常。我曾尝试按照RestKit文档使用RKObjectManager删除孤立对象,但它似乎不起作用。以下是我所拥有的,任何洞察都会很好: 对象映射 注意-运行映射或进行API调用时,addFetchRequestBlock不会执行 拉取数据 设置NSFetchedResultsControllerIos RestKit、NSFetchedResultsController和删除孤立对象,ios,objective-c,restkit,nsfetchedresultscontroller,restkit-0.20,Ios,Objective C,Restkit,Nsfetchedresultscontroller,Restkit 0.20,我已使用NSFetchedResultsController设置RestKit,并使用API和核心数据正常工作,以显示信息。除了在服务器上删除本地对象后删除这些对象之外,所有这些都工作正常。我曾尝试按照RestKit文档使用RKObjectManager删除孤立对象,但它似乎不起作用。以下是我所拥有的,任何洞察都会很好: 对象映射 注意-运行映射或进行API调用时,addFetchRequestBlock不会执行 拉取数据 设置NSFetchedResultsController 这可能是缓存问
这可能是缓存问题吗?还是我设置不正确?获取请求块中的路径模式
/api/v1/:userID/locations
与获取请求不匹配
但您的主要问题是,要使用fetch请求块,对象管理器需要运行该请求。实际上,您正在显式地创建请求和操作并执行。您应该完全创建和配置对象管理器,并使用它来发出请求(也应该少编写代码)。第一个问题:Doh!第二个问题:这很有道理。我并没有从这些例子中明确地收集到这一点。谢谢
+ (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;
}
+(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*)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;
}