答えて

1

は、最初の更新必要

func connectionCoreData() { 
     let fetchRequest = NSFetchRequest<PersonalClass>(entityName: "PersonalBase") 
     let sortDescriptor = NSSortDescriptor(key: "personName", ascending: true) 
     fetchRequest.sortDescriptors = [sortDescriptor] 
     if let managerObjectContext = (UIApplication.shared.delegate as? AppDelegate)?.managedObjectContext { 
      fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managerObjectContext, sectionNameKeyPath: nil, cacheName: nil) 
      fetchResultController.delegate = self 
      do { 
       try fetchResultController.performFetch() 
       personalArray = fetchResultController.fetchedObjects! 
       self.tableView.reloadData() 
      } catch { 
       print(error) 
      } 
     } 
    } 

、あなたはmanagedObjectContextは、単一のスレッドで実行されていることを念頭に置く必要があります。同じスレッド上のロードされたオブジェクトにアクセス/編集する必要があります。

あなたの場合、メインスレッドにロードするオブジェクトと対話します。たとえば、ロードされたデータベースオブジェクトはtableViewとなり、これはメインスレッドで実行する必要があります。これにより、managedObjectContextはメインスレッド上で動作するMainContextTypeになります。

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html

あなたはそれがバッチ内のオブジェクトをロードするため、メインスレッド上でNSFe​​tchedResultsControllerを実行しているのを恐れてはなりません。ただし、FetchedResultsコントローラは必要に応じて使用していません。コード内にこれらの2行を入れてはいけません。

personalArray = fetchResultController.fetchedObjects! 
self.tableView.reloadData() 

このメソッドfetchResultController .objectAtIndexPath(indexPath)を使用して、読み込まれたオブジェクトにアクセスする必要があります。

これはNSFetchedResultsController

class ViewController: UITableViewController NSFetchedResultsControllerDelegate{ 

    lazy var fetchedResultsController: NSFetchedResultsController = { 
     let fetchRequest = ... //Create the fetch request 
     let sortDescriptor = ... //Create a sortDescriptor 
     let predicate = ...//Create the predicate if you want to filter the results 
     fetchRequest.predicate = predicate 

     let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: mainContext, sectionNameKeyPath: nil, cacheName: nil) 
     fetchedResultsController.delegate = self 
     return fetchedResultsController 
    }() 

    override fun viewDidLoad(){ 
     super.viewDidLoad() 
     do { 
      try self.fetchedResultsController.performFetch() 
     }catch { 
      print(error) 
     } 
    } 

    func controllerWillChangeContent(controller: NSFetchedResultsController) { 
     tableView.beginUpdates() 
    } 

    func controllerDidChangeContent(controller: NSFetchedResultsController) { 
     tableView.endUpdates() 
    } 

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
     switch (type) { 
     case .Insert: 
      if let indexPath = newIndexPath { 
       tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
      } 
      break; 
     case .Delete: 
      if let indexPath = indexPath { 
       tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
      } 
      break; 
     case .Update: 
      if let indexPath = indexPath { 
       tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
      } 
      break; 
     case .Move: 
      if let indexPath = indexPath, newIndexPath = newIndexPath { 
       tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath) 
      } 
      break; 
     } 
    } 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if let sections = fetchedResultsController.sections { 
      let sectionInfo = sections[section] 
      return sectionInfo.numberOfObjects 
     } 
     return 0 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let obj = fetchedResultsController.objectAtIndexPath(indexPath){ 
     .... 
    } 

} 
+0

感謝を使用する方法の例ですが、私はコアデータに32K行を持っている(NSFetchedResultsControllerでベースのロードは数秒かかり、と私は、検索を追加したい場合このベース)と私はより速くコアデータの負荷をする必要が何をする必要がありますか?コアデータにもっと多くのリレーションシップを追加して、グループごとにリレーションを介してベースをロードしようとしてもよいでしょうか、それともこの時間もかかりますか? – Skie

+0

本当にあなたがしようとしていることに依存します...あなたはあなたのクエリを最適化しようとする必要があります。インデックス作成を使用します。しかし、NSFetchedresultscontrollerをバックグラウンドコンテキストに置くことは、そのように使われることを意図していないため、悪夢です。 – ELKA

関連する問題