[問題] Core Data Fetch與GCD的問題

看板MacDev作者 (狗狗)時間13年前 (2012/09/11 03:06), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/3 (看更多)
想問一個關於CoreData的Fetch和GCD的問題 因為我的database有11萬筆資料 所以我在AppDelegate 的didFinishLaunchingWithOptions: 裡面用GCD進行Fetching ==in AppDelegate -application:didFinishLaunchingWithOptions: // 把fetchRC的delegate設為tableVC self.fetchedResultsController.delegate = self.tableViewController; // 用GCD進行fetch dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSError *error = nil; if (![self.fetchedResultsController performFetch:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } self.fetchObjects = [self.fetchedResultsController fetchedObjects]; // 加入這行之後 tableView才會更新 [self.tableViewController.tableView reloadData]; }); == 這樣的話App開啟的時候會直接顯示我的table view 但是等fetch跑完之後table view的data並沒有更新 tableViewController裡面的NSFetchedResultsControllerDelegate methods被沒有被呼叫 於是我在GCD裡的最後一行 加入了reloadDta強迫tableView重新載入資料 但是reloadData執行完後 table view卻等了5秒鐘才更新 請問 1) NSFetchedResultsControllerDelegate methods沒被呼叫是正常的嗎? 2) 有沒有辦法加速在reload完之後 更新tableView的速度? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.198.45.233 ※ 編輯: leondemon 來自: 60.198.45.233 (09/11 03:34)

09/11 06:50, , 1F
跟view相關的要丟到main queue裡面做
09/11 06:50, 1F

09/11 06:51, , 2F
你可以把tableView reloadData那行用dispatch_async
09/11 06:51, 2F

09/11 06:51, , 3F
dispatch_get_main_queue() 包起來
09/11 06:51, 3F

09/11 17:00, , 4F
thanks!
09/11 17:00, 4F
文章代碼(AID): #1GJZf7OR (MacDev)
文章代碼(AID): #1GJZf7OR (MacDev)