我需要從隨機順序的數據庫中獲取對象。 首先,我生成隨機的「ID」 S成NSArray
並將它們設置爲斷言:(核心數據)如何爲NSFetchedResultsController進行隨機排序?
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(self.id IN %@)", randomIds];
// The example of randomIds contents (always different):
// <__NSArrayM 0xd1cc4e0> (185,51,69,25,33,135,136,97,157,112,145,132,56,15,159,70,88,6,72,82)
但是到了這個時候我的成績依然排序,因爲NSFetchedResultsController
堅持設置「sortDescriptors」,其FetchRequest
:
NSSortDescriptor *sortingDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES];
fetchRequest.sortDescriptors = @[sortingDescriptor];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
但我需要以隨機順序讀取結果:同在「randomIds」陣列。
我還沒有找到任何合理的解決方案,這個問題呢。你會如何解決它?有人有機會得到任何解決方案嗎?
已經嘗試過比較塊 - 是的,它禁止與核心數據一起使用,很遺憾。當然,我可以輕鬆地將結果數組的元素轉換爲另一個數組,但我在UITableView的'cellForRowAtIndexPath'中使用了NSFetchedResultsController對象(我真的需要這樣做) – AlG