核心数据关系、NSPredicates 和 NSFetchedResultsController
这让我发疯了一整天。
我有一个奇怪的错误,我想我已经将其范围缩小到 NSPredicate。我有两个实体:列表和人员。 List 与 Person 之间存在一对多关系,称为“persons”,而 Person 与 List 之间存在一对多关系,称为“lists”。
我将一个 List 对象传递给我的 tableview 控制器。然后我希望该表视图控制器显示属于该列表对象的人员。我正在使用 NSFetchedResultsController 执行此操作。
设置 NSFRC 时,我有以下代码(为了清楚起见,省略了内存管理)。有问题的列表是 myList
:
// Create the request and set it's entity
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Create a predicate to get the persons that belong to this list
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY lists == %@)", myList];
// Assign this predicate to the fetch request
[fetchRequest setPredicate:predicate];
// Define some descriptors
NSSortDescriptor *locationDescriptor = [[NSSortDescriptor alloc] initWithKey:@"location" ascending:YES];
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:locationDescriptor, lastNameDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"location" cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
我认为问题出在这一行(因为如果我删除它,它就会消失):
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY lists == %@)", myList];
发生的情况是当父视图将 myList
传递给tableview 控制器,模拟器只是挂起。控制台中没有崩溃日志或其他任何内容。就好像只花了很长的时间来整理 NSFRC 一样。
这是我使用的谓词有问题吗?
This has been driving me nuts all day.
I have a weird bug that I think I have narrowed down to an NSPredicate. I have two entities: List and Person. List has a to-many relationship to Person called persons and Person has a to-many relationship to List called lists.
I pass to my a tableview controller a List object. I then want that tableview controller to display the Persons that belong to that list object. I am doing this with a NSFetchedResultsController.
When setting up the NSFRC, I have the following code (memory management omitted for clarity). The List in question is myList
:
// Create the request and set it's entity
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Create a predicate to get the persons that belong to this list
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY lists == %@)", myList];
// Assign this predicate to the fetch request
[fetchRequest setPredicate:predicate];
// Define some descriptors
NSSortDescriptor *locationDescriptor = [[NSSortDescriptor alloc] initWithKey:@"location" ascending:YES];
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:locationDescriptor, lastNameDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"location" cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
I think the problem is with this line (because it disappears if I remove it):
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY lists == %@)", myList];
What is happening is when the parent view passes myList
to the tableview controller, the simulator just hangs. No crash log in the console or anything. It's almost as if it's just taking AGES to sort out the NSFRC.
Is this a problem with the predicate I'm using?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您可以从传递到 tableViewController 的列表中获取
Person
时,是否需要使用NSFetchedResultsController
?Do you you need to use
NSFetchedResultsController
when you can obtain thePerson
s from the list passed into the tableViewController?你是对的,你可以只使用
myList.persons
,在这种情况下不需要NSFetchedResultsController
。You are correct, you can just use
myList.persons
, anNSFetchedResultsController
is not necessary in this situation.感谢您关于使用 NSSet 的建议。经过几个小时的错误跟踪后,我意识到问题出在表视图的 cellForIndexPath 方法中(因此,与 NSFRC 无关)。
Thanks for the suggestions re: using an NSSet. After hours of bug-tracking I realised that the problem lie in my cellForIndexPath method of the table view (so, unrelated to the NSFRC).