0

这让我整天发疯。

我有一个奇怪的错误,我认为我已经缩小到一个 NSPredicate。我有两个实体:List 和 Person。List 与名为 Person 的 Person 具有一对多关系,而 Person 与名为 lists 的 List 具有一对多关系。

我将一个 List 对象传递给我的 tableview 控制器。然后,我希望该 tableview 控制器显示属于该列表对象的 Persons。我正在使用 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 控制器时,模拟器只是挂起。控制台中没有崩溃日志或任何东西。就好像它只需要 AGES 来整理 NSFRC。

这是我使用的谓词的问题吗?

4

3 回答 3

1

可以从传入tableViewController的列表NSFetchedResultsController中获取s的时候需要使用吗?Person

NSSet *people = myList.persons;
于 2010-07-05T03:18:00.980 回答
1

你是对的,你可以只使用myList.persons, anNSFetchedResultsController在这种情况下是没有必要的。

于 2010-07-05T03:23:55.583 回答
0

感谢您的建议:使用 NSSet。经过数小时的错误跟踪,我意识到问题出在我的表格视图的 cellForIndexPath 方法中(因此,与 NSFRC 无关)。

于 2010-07-05T16:29:12.780 回答