核心数据关系、NSPredicates 和 NSFetchedResultsController

发布于 2024-09-08 07:47:25 字数 1897 浏览 3 评论 0原文

这让我发疯了一整天。

我有一个奇怪的错误,我想我已经将其范围缩小到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放手` 2024-09-15 07:47:25

当您可以从传递到 tableViewController 的列表中获取 Person 时,是否需要使用 NSFetchedResultsController

NSSet *people = myList.persons;

Do you you need to use NSFetchedResultsController when you can obtain the Persons from the list passed into the tableViewController?

NSSet *people = myList.persons;
不回头走下去 2024-09-15 07:47:25

你是对的,你可以只使用myList.persons,在这种情况下不需要NSFetchedResultsController

You are correct, you can just use myList.persons, an NSFetchedResultsController is not necessary in this situation.

鱼窥荷 2024-09-15 07:47:25

感谢您关于使用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文