我有一个名为“sensor”的强指针,指向“SensorDB”类的对象。
我尝试创建一个NSFetchedResultController,以便在“sensor”对象内部发生更改时通知我的UIViewController。
为了将所需对象放入我的NSFetchedResultController,我创建了以下NSPredicate:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self == %@", self.sensor];
目标是,我在NSFetchedResultController中只有一个对象,并获得有关任何更新的通知,以便我可以更新我的GUI。
但是,如果我运行我的应用程序,当我尝试分配并初始化NSFetchedResultController时,应用程序崩溃。
这是我的NSFetchedResultController代码:
- (NSFetchedResultsController *)fetchedResultsControllerSensor
{
if (_fetchedResultsControllerSensor != nil) {
return _fetchedResultsControllerSensor;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SensorDB" inManagedObjectContext:dbHandler.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self == %@", self.sensor];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:dbHandler.managedObjectContext sectionNameKeyPath:nil cacheName:@"SensorCache"];
self.fetchedResultsControllerSensor = theFetchedResultsController;
_fetchedResultsControllerSensor.delegate = self;
return _fetchedResultsControllerSensor;
}
感谢您的帮助或其他/更好的方法
Linard
答案 0 :(得分:0)
我想,我不需要排序描述符,因为我只有一个结果对象,但看起来NSFetchedResultController的初始化崩溃了,如果还没有一个NSSortDescriptor。
现在我的NSPredicate和NSSortDescriptor:
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(SELF = %@)", self.sensor];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];