我有一個強大的指針,其名稱爲「Sensor」,名稱爲「SensorDB」類的對象。使用CoreData爲同等對象創建NSPredicate
我試着創建一個NSFetchedResultController,以便我的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