gpt4 book ai didi

ios - 具有核心数据和 Restkit 的多 View Controller

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:03 25 4
gpt4 key购买 nike

我想我可能在这里遗漏了一些明显的东西,因为这一定是 RestKit 的一个非常常见的用例。

我想要两个 View Controller ,每个都只是将 NSFetchedResultsController 粘附到 UITableView,假设第一个显示帖子的时间轴,第二个显示特定用户的帖子列表。 我需要为每个 View Controller 提供不同的帖子列表,但我不知道如何使用 RestKit 获取这些列表。

目前,我在每个 View 上使用相同的 NSManagedObjectContext,这意味着如果我有一个帖子存在于 View Controller B 但不在 View Controller A 中,如果我在加载 View Controller B 后返回 View Controller A,那对 View Controller B 来说应该是唯一的帖子现在也显示在 View Controller A 中。

我想我应该做的是为每个 View 使用不同的 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)]];

These are my Core Data entities

(StackOverflow 不允许我发布图片……)

在我的 View Controller 中,这是我的fetchedResultsController getter

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

这就是我从时间线 View Controller 发出请求的方式

[[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.

这就是我向用户 View Controller 发出请求的方式

NSString *path = [NSString stringWithFormat:@"/_/%@.json", self.user.username];
[[RKObjectManager sharedManager] getObjectsAtPath:path parameters:nil success:success failure:failure];

最佳答案

首先,不要以/ 开始路径模式。您也不需要在请求路径的开头使用它。

对于实际问题,当您定义NSFetchedResultsController 时,您还需要为fetchRequest 定义一个NSPredicate。这个谓词将允许 FRC 仅过滤 View Controller 实际感兴趣的信息(即使您的数据模型实际上包含比这更多的数据)。谓词将基于 Controller 传递的某个值,它应该显示...的详细信息

检查谓词文档 here .

关于ios - 具有核心数据和 Restkit 的多 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16702919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com