我的核心数据定义如下:用户有很多事件; event只有一个用户关系;
用户和事件都是核心数据实体 . 用户实体通过故事板segue传入 .
我正在尝试配置NSPredicate以仅为该特定用户的事件填充该用户的详细信息UITableView .
到目前为止我已经尝试过
//does not work
NSPredicate* onlyThisUserPredicate = [NSPredicate predicateWithFormat:@"user == %@",self.appUser];
//does not work
NSPredicate* onlyThisUserPredicate = [NSPredicate predicateWithFormat:@"SELF.user == %@",self.appUser];
What is the proper syntax to compare events and only return those that have user object equal to the specified user object?
更新:
我正在尝试使用这种获取的结果控制器向用户添加事件:
-(NSFetchedResultsController*)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:[Event managedObjectContext]];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
//I need to configure this user
NSPredicate* onlyThisUserPredicate = [NSPredicate predicateWithFormat:@"user = %@",self.appUser];
// The first sort key must match the section name key path key if present, otherwise the initial dataset would be messed up: rows in incorrect sections
NSString* firstSortKey = @"createDate";
NSSortDescriptor *firstSortDescriptor = [[NSSortDescriptor alloc] initWithKey:firstSortKey ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:firstSortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setPredicate:onlyThisUserPredicate];
// 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:[Event managedObjectContext] sectionNameKeyPath:nil cacheName:@"Events"];
self.fetchedResultsController = aFetchedResultsController;
aFetchedResultsController.delegate = self;
// [aFetchedResultsController release];
[sortDescriptors release];
[fetchRequest release];
NSError *error = nil;
if (![__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 回答
好的,我可以想到的一些事情可能会导致这种行为 .
首先,您是否在此函数中验证了self.appUser的值?它是否符合你的期望?
其次,您确定您的 Headers 是最新的并包含在此文件中吗?有时候,当我的 Headers 与coredata模型不一致时,我会遇到奇怪的行为 .
那么这个谓词对于User实体是否正确?如果是这样,你试试这个:
然后,您可以通过以下方式访问
如果您已经从Core Data存储中检索了“user”,那么您应该只需遵循该关系即可访问其事件 - 无需执行单独的获取请求:
另一方面,如果
self.appUser
不是托管对象,则在谓词中使用==
运算符可能是问题所在 . 因此,我假设self.appUser
只是一个包含用户名称的字符串,而不是数据存储中的用户对象 . 然后你'd use the '喜欢谓词中的'运算符:此外,请确保您在获取请求中指定了正确的实体 . 对于您所描述的内容,您应该使用事件实体的实体描述进行提取 .