RestKit,NSFetchedResultsController和删除孤立对象

时间:2013-12-23 15:37:02

标签: ios objective-c restkit nsfetchedresultscontroller restkit-0.20

我有RestKit设置&使用NSFetchedResultsController显示信息,使用API​​和Core Data正常工作。一切都运行良好,除了在服务器上删除本地对象后删除它们。我曾尝试使用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 个答案:

答案 0 :(得分:1)

获取请求块中的路径模式/api/v1/:userID/locations与您的GET请求不匹配。

但是你的主要问题是,对于要使用的获取请求块,对象管理器需要运行请求。因为它是你明确地创建请求和操作并执行。您应该完全创建和配置对象管理器并使用它来发出请求(这也应该是您编写的代码更少)。