gpt4 book ai didi

ios - 带分组的核心数据 NSFetchRequest

转载 作者:可可西里 更新时间:2023-11-01 04:23:47 40 4
gpt4 key购买 nike

我有一个获取请求,应该生成一个包含结果中的两个部分的 TableView 。

我的模型:

@interface Player : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * isFacebookFriend;

此模型上的获取请求应该导致一个部分中的人在一个部分中是 isFacebookFriend == YES,而在第二部分中是 isFacebookFriend == NO。

我试过

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
HBAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
[fetchRequest setSortDescriptors:@[nameDescriptor]];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToGroupBy:@[@"isFacebookFriend"]];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:@"playerCache"];
NSError *error;
[_fetchedResultsController performFetch:&error];

但这并没有做到。错误是:

2013-06-12 12:27:28.364 TwentyQuestions[25015:c07] Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0xb676aa0>.
2013-06-12 12:27:31.119 TwentyQuestions[25015:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'SELECT clauses in queries with GROUP BY components can only contain properties named in the GROUP BY or aggregate functions ((<NSAttributeDescription: 0xc382320>), name hasRegisteredForGame, isOptional 1, isTransient 0, entity Player, renamingIdentifier hasRegisteredForGame, validation predicates (
), warnings (
), versionHashModifier (null)
userInfo {
}, attributeType 800 , attributeValueClassName NSNumber, defaultValue (null) is not in the GROUP BY)'

最佳答案

要创建带有部分的 TableView ,您必须使用 sectionNameKeyPath 参数NSFetchedResultsController。您还必须添加一个(第一个)排序描述符来排序根据部分名称键路径。

NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSSortDescriptor *isFacebookFriendDescriptor = [[NSSortDescriptor alloc] initWithKey:@"isFacebookFriend" ascending:NO];
[fetchRequest setSortDescriptors:@[isFacebookFriendDescriptor, nameDescriptor]];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:appDelegate.managedObjectContext
sectionNameKeyPath:@"isFacebookFriend"
cacheName:@"playerCache"];

您不必设置 setPropertiesToGroupBy,我也不建议设置NSDictionaryResultType 因为它会禁用自动更新通知。

关于ios - 带分组的核心数据 NSFetchRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17073659/

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