我认为我可能在这里遗漏了一些明显的东西,因为这必须是 RestKit 的一个非常常见的用例。
我想要两个视图控制器,每个视图控制器都只是将 NSFetchedResultsController 粘合到 UITableView,假设第一个显示帖子的时间线,第二个显示某个用户的帖子列表。我需要为每个视图控制器提供不同的帖子列表,但我不知道如何使用 RestKit 获取这些列表。
目前,我在每个视图上使用相同的 NSManagedObjectContext,这意味着如果我有一个帖子存在于视图控制器 B 中但不在视图控制器 A 中,如果我在加载视图控制器 B 后返回视图控制器 A,那么该帖子应该对视图控制器 B 是唯一的现在也显示在视图控制器 A 中。
我认为我应该为每个视图使用不同的 NSManagedObjectContexts,共享一个 RKManagedObjectStore,但我不知道如何让它工作。
这些是我的映射:
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
[userMapping addAttributeMappingsFromDictionary:@{
@"avatar_url": @"avatarURL",
@"id": @"userID",
@"first_name": @"firstName",
@"last_name": @"lastName",
@"username": @"username"
}];
userMapping.identificationAttributes = @[@"userID"];
RKEntityMapping *postMapping = [RKEntityMapping mappingForEntityForName:@"Post" inManagedObjectStore:managedObjectStore];
[postMapping addAttributeMappingsFromDictionary:@{
@"id": @"postID",
@"content_url": @"contentURL",
@"created_at": @"createdAt"
}];
postMapping.identificationAttributes = @[@"postID"];
[postMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"creator" toKeyPath:@"creator" withMapping:userMapping]];
[userMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"posts" toKeyPath:@"posts" withMapping:postMapping]];
[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Post class] pathPattern:@"/posts\\.json" method:RKRequestMethodGET]];
[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Post class] pathPattern:@"/posts\\.json" method:RKRequestMethodPOST]];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:postMapping
pathPattern:@"/posts.json"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:userMapping
pathPattern:@"/_/:username.json"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
这些是我的核心数据实体
(StackOverflow 不允许我发布图片……)
在我的视图控制器中,这是我的fetchedResultsController
吸气剂
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
这就是我从时间线视图控制器发出请求的方式
[[RKObjectManager sharedManager] getObjectsAtPath:@"/posts.json" parameters:nil success:success failure:failure];
// The success and failure blocks don't do all that much, so I've just left them out.
这就是我向用户视图控制器发出请求的方式
NSString *path = [NSString stringWithFormat:@"/_/%@.json", self.user.username];
[[RKObjectManager sharedManager] getObjectsAtPath:path parameters:nil success:success failure:failure];