接近以下內容的正確方法是什麼? 要獲得CoreData對象的列表,我有一個'datamanager',我在其中創建一個NSFetchResultController,執行搜索,並使用這些對象生成NSFetchResultsController。通過使用保留取得autorelease對象的所有權?
因爲可以有多個表,所以在每個請求上都需要創建一個新的FetchedResultsController,以便允許自動更新它們關聯的tableViews。
我所做的是在DataManager中創建FetchedResultController,然後autorelease它。 在TableViewController中,我保留它(假設它將擁有它)。
NSFetchedResultsController* objects = [[delegate dataManager] getMenu:parentMenu];
_objects = [objects retain];
這將調用DataManager的:
- (NSFetchedResultsController*) getMenu:(DOMenuItem*) parentMenu {
NSPredicate* predicate;
if(parentMenu == nil){
predicate = [NSPredicate predicateWithFormat:@"(parent == NULL)", parentMenu];
} else {
predicate = [NSPredicate predicateWithFormat:@"(parent == %@)", parentMenu];
}
[NSFetchedResultsController deleteCacheWithName:_cacheName];
NSFetchedResultsController* aFetchedResultsController = [self createFetchedResultsController];
[aFetchedResultsController.fetchRequest setPredicate:predicate];
[aFetchedResultsController.fetchRequest setFetchLimit:0];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:_defaultSortField ascending:YES] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, nil] autorelease];
[aFetchedResultsController.fetchRequest setSortDescriptors:sortDescriptors];
// Fetch the records and handle an error
NSError *fetchError;
if (![aFetchedResultsController performFetch:&fetchError]) {
// Handle the error.
// This is a serious error and should advise the user to restart the application
NSLog(@"Fetching data error: %@", [fetchError localizedDescription]);
}
return aFetchedResultsController;
}
- (NSFetchedResultsController *) createFetchedResultsController {
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:_objectName inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:_defaultSortField ascending:NO] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, nil] autorelease];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:_sectionNameKeyPath cacheName:_cacheName];
return [aFetchedResultsController autorelease];
}
不過,我在運行的情況下,我得到這個FetchedResultController一個dealloc的錯誤就行通話。
if(indexPath.section == [[_objects sections] count] + 1){
的消息是:
* - [NSFetchedResultsController節]:消息發送到釋放實例0xe3de1b0
有什麼錯用這種方法? 如果是這樣,將創建類的所有權「轉移」到需要保留它的類的首選方法是什麼?
編輯: 當我在返回fetchedResultsController時放置'retain'而不是'autorelease'時,它一切正常,但這似乎不正確的編碼實踐,因爲'所有者'不釋放目的。我不認爲'複製',或者與ManagedObjects一起使用,還是這樣做?
你的意思是什麼「我得到一個Dealloc錯誤」?什麼是錯誤,在哪裏? – 2012-03-22 17:24:13
您不顯示調用createFetchedResultController的代碼。 – 2012-03-22 17:25:59
我添加了代碼並釋放了消息。先謝謝您的幫助! – 2012-03-22 18:49:25