2017-06-27 67 views
0

我嘗試使用swift 3獲取結果控制器,但它不起作用。我看到,我的實體被添加到核心數據與我自己的獲取請求,但FRC沒有看到他們。如果我重新啓動我的應用程序,新的元素將出現在表格中。獲取結果控制器不會在運行時更新表

創造FRC的:

func getFRCForChats() -> NSFetchedResultsController<Conversation>{ 
    var fetchedResultsController: NSFetchedResultsController<Conversation> 

    let fetchRequest: NSFetchRequest<Conversation> = Conversation.fetchRequest() 
    let sortDescriptor = NSSortDescriptor(key: "conversationID", ascending: true) 

    fetchRequest.sortDescriptors = [sortDescriptor] 

    fetchedResultsController = NSFetchedResultsController<Conversation>(fetchRequest: 
     fetchRequest, managedObjectContext: container.viewContext, sectionNameKeyPath: nil, cacheName: nil) 

    return fetchedResultsController 
} 

使用FRC的:

var fetchedResultsController: NSFetchedResultsController<Conversation>! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 

    coreDataService = CoreDataService() 
    fetchedResultsController = coreDataService.getFRCForChats() 
    fetchedResultsController.delegate = self 
    try! fetchedResultsController.performFetch() 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if(!fetchedResultsController.sections!.isEmpty) { 
     if let sectionInfo = fetchedResultsController.sections?[section]{ 
      return sectionInfo.numberOfObjects 
     } else { print("Unexpected Section") } 
    } 
    return 0 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    if let count = fetchedResultsController.sections?.count { 
     return count 
    } else { 
     return 0 
    } 
} 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "ChatCell", for: indexPath) as!ChatTableViewCell 

    //cell.name.text = cells[indexPath.row] 

    let conversation = fetchedResultsController.object(at: indexPath) 

    if let participants = conversation.participants as? Set<User> { 

     conversation.managedObjectContext?.performAndWait { 
      for user in participants{ 
       cell.name.text = user.name 
       break 
      } 
     } 
    } 


    return cell 

} 

添加新的實體:

var k = 2 
@IBAction func createConversation(_ sender: Any) { 
    coreDataService.insertConversation(id: k) 
    k += 1 
} 



func insertConversation(id: Int) { 

    container.performBackgroundTask { (context) in 


    let user = User.findOrInsertUser(id: id, name: "Andrew", mobilePhone: 234567, avatar: nil, inContext: context) 

    _ = Conversation.findOrInsertConversation(id: id + 100, summa: Double(12+id), users: [user], transactions: [], inContext: context) 

     context.saveThrows() 
    } 

    let request: NSFetchRequest<Conversation> = Conversation.fetchRequest() 

    container.viewContext.perform { 
     if let results = try? self.container.viewContext.fetch(request) { 
      print("\(results.count) TweetMs") 
      for result in results{ 
       print(result.conversationID, result.summa) 
      } 
     } 
    } 
} 

代表:

extension ChatsViewController: NSFetchedResultsControllerDelegate { 
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { 
    tableView.beginUpdates() 
} 

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { 
    tableView.endUpdates() 
} 

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { 
    switch type { 
    case .delete: 
     if let indexPath = indexPath { 
      tableView.deleteRows(at: [indexPath], with: .automatic) 
     } 
    case .insert: 
     if let newIndexPath = newIndexPath { 
      tableView.insertRows(at: [newIndexPath], with: .automatic) 
     } 
    case .move: 
     if let indexPath = indexPath { 
      tableView.deleteRows(at: [indexPath], with: .automatic) 
     } 

     if let newIndexPath = newIndexPath { 
      tableView.insertRows(at: [newIndexPath], with: .automatic) 
     } 
    case .update: 
     if let indexPath = indexPath { 
      tableView.reloadRows(at: [indexPath], with: .automatic) 
     } 
    } 
} 

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) { 
    switch type { 
    case .delete: 
     tableView.deleteSections(IndexSet(integer: sectionIndex), 
           with: .automatic) 
    case .insert: 
     tableView.insertSections(IndexSet(integer: sectionIndex), 
           with: .automatic) 
    case .move, .update: break 
    } 
} 
} 

編輯:如果我在每次編輯上下文之後調用performFetch(),則表不會重新調用委託。

回答

1

Apple Docs作爲狀態:

automaticallyMergesChangesFromParent

一個布爾值,表示上下文是否自動合併保存到其持久存儲協調器或父上下文 變化。

嘗試將其設置到true

container.viewContext.automaticallyMergesChangesFromParent = true 

根本上解決了類似的問題對我來說至少。

相關問題