我有一個UITableView,顯示名爲「Documents」的大量實體的子集。該子集由另一個實體「選擇」定義。選擇被命名,有序的文件列表。如何以編程方式切換UITableView的NSFetchedResultsController(或其謂詞)?
它工作正常,除非我想在運行時更改顯示的選擇。我只得到一個空白列表。
基本上,我需要改變我的NSFetchedResultsController持有的謂詞,以便新的謂詞使用另一個Selection。我無法讓它工作。我最後一次嘗試就是要完全擺脫NSFetchedResultsController的並對其重新分配:
- (void) displaySelection:(Selection *)aSet
{
self.currentSelection = aSet;
self.fetchedResultsController = nil;
// methods here don't all use the property but directly the ivar, so we must trigger the getter
[self fetchedResultsController];
[self.tableView reloadData];
}
當然,在NSFetchedResultsController吸氣做正確的事:
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController != nil) { return fetchedResultsController; }
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"DocInSelection" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"selection.identifier like %@", currentSelection.identifier];
[fetchRequest setPredicate:predicate];
<snip>
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
<snip>
return fetchedResultsController;
}
此代碼工作的第一次,因爲初始選擇已設置。但是,調用displaySelection:
時,tableview變爲空白。
一個非常類似的問題被問在NSFetchedResultsController fetch request - updating predicate and UITableView
而答案是擺脫NSFetchedResultsController的。我不想這樣做,因爲NSFetchedResultsController在這裏帶來了很多有用的好東西(例如緩存,部分加載......)。問題仍然存在:如何在由NSFetchedResultsController支持的UITableView中「切換」數據,其中「switch」意味着具有不同的謂詞,或者甚至(不在我的情況下)具有不同的實體。
爲了完整起見,請注意,由於從Selection到Document的多對多關係是有序的,所以它通過一個稱爲DocInSelection的中間輕量級實體進行處理,該實體具有「排序」屬性和兩個很多對文檔和選擇的一對一關係。
感謝您的任何建議。
如果你打算deleteCacheWithName:@「Root」每一次,爲什麼甚至創建緩存。如果不使用緩存,則傳入緩存名稱爲nil。儘管緩存是首先使用NSFRC的好處之一。 – bandejapaisa 2011-08-15 13:05:26
我想,雖然當前選擇(可能很大)保持不變,但緩存由「NSFetchedResultController」使用,效果良好。 「每一次」可能都不是那麼經常。 Apple還在[修改提取請求]中說過(http://developer.apple.com/library/iOS/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html)「如果您正在使用緩存,請將其刪除(使用deleteCacheWithName :)。「蘋果還增加了「通常情況下,如果您要更改獲取請求,則不應該使用緩存」。測試應該有助於決定緩存是否有益於此。 – 2011-08-18 08:17:28