2014-01-11 362 views
3

會發生什麼:NSFetchedResultsController非常緩慢

我的應用程序是掛大約3秒鐘,每當我創建一個新的核心數據對象。該對象不是一個大或複雜的對象。

-

的詳細信息:

[[SharedCoreDataBackend managedObjectContext] save:&error]; 

這個結果:

上的按鈕被按下的創建一個新的對象,有屬性進行設置,然後用保存在NSFetchedResultsController被更新如下。我已經收窄放緩下面的代碼行:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 

    [self.tableView beginUpdates]; 

    [self.tableView deleteRowsAtIndexPaths:self.deletedRowIndexPaths withRowAnimation:UITableViewRowAnimationNone]; 
    [self.tableView insertRowsAtIndexPaths:self.insertedRowIndexPaths withRowAnimation:UITableViewRowAnimationNone]; 
    [self.tableView reloadRowsAtIndexPaths:self.updatedRowIndexPaths withRowAnimation:UITableViewRowAnimationNone]; 

    [self.tableView endUpdates]; 
} 

-

所以...

任何想法的問題是什麼?是否有更多的測試或代碼可以發佈,有助於解決此問題?

+0

是否有任何理由,你不使用標準的鍋爐板代表方法(如Duncan的答案所示)? –

回答

5

與委託方法像這樣的嘗試......

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{ 
     [self.tableView beginUpdates]; 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath 
{ 
     switch(type) { 
      case NSFetchedResultsChangeInsert: 
      {  
       [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      } 
       break; 

      case NSFetchedResultsChangeDelete: 
      { 
       [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      } 
       break; 

      case NSFetchedResultsChangeUpdate: 
      { 
       [self configureCell:tableView cell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      } 
       break; 

      case NSFetchedResultsChangeMove: 
      { 
       [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
       [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      } 
       break; 
     } 
} 
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
     [self.tableView endUpdates]; 
} 
0

對於我的同胞Swifters希望我能爲你節省幾秒鐘:

func controllerWillChangeContent(controller: NSFetchedResultsController) { 
    myTableView?.beginUpdates() 
} 


func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
    switch(type) { 
     case .Insert: 
      self.myTableView?.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade) 
     case .Delete: 
      self.myTableView?.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) 
     case .Update: 
      self.configureCell((myTableView?.cellForRowAtIndexPath(indexPath!)), atIndexPath: indexPath!) 
     case .Move: 
       myTableView?.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) 
       myTableView?.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade) 
    } 
} 

func configureCell(cell: UITableViewCell?, 
    atIndexPath indexPath: NSIndexPath) { 
    //YOUR CELL UPDATE CODE 
} 

func controllerDidChangeContent(controller: NSFetchedResultsController) { 
    myTableView?.endUpdates() 
}