3

我认为我可能在这里遗漏了一些明显的东西,因为这必须是 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];
4

2 回答 2

1

首先,不要以/. 您也不需要它在请求路径的开头。

对于实际问题,在定义 your 时NSFetchedResultsController,还需要NSPredicatefetchRequest. 此谓词将允许 FRC 仅过滤视图控制器实际感兴趣的信息(即使您的数据模型实际上包含的数据多于该信息)。谓词将基于控制器传递的某个值,它应该显示...的详细信息

在此处查看谓词文档。

于 2013-05-23T16:12:37.380 回答
0

@Ross,你做错的是你没有过滤你的请求,你只是得到了所有的请求,你必须使用 NSPredicate 来过滤它。

您将需要在视图控制器 A 和视图控制器 B 中有不同的过滤器。

您可以创建类似的内容,例如从用户那里获取所有帖子:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"creator.username like %@",
       self.user.username];

要获得所有最后的帖子,您甚至不需要谓词,您的排序描述符必须解决问题。

当您需要向它发送谓词时,这就是您的方法的外观,或者您可以直接将其添加到您已有的方法中。现在是你的选择。

- (NSFetchedResultsController *)fetchedResultsController:(NSPredicate*)predicate
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    [fetchRequest setPredicate:predicate];

    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;
}

编辑 选项3 - 您可以拥有一个关注的用户列表,并使用用户过滤您的请求,并获取您关注的用户的所有帖子。

您需要首先获取所有关注的用户,然后您可以使用看起来像这样的子查询来评估您的帖子(我没有测试它,也许有问题)

[NSPredicate predicateWithFormat:@"(SUBQUERY(creator, $x, $x.username IN %@).@count > 0)", followedUsernames];

followedUsernames包含关注用户的所有用户名的数组在哪里。

于 2013-05-23T17:32:26.050 回答