Multipeer Connectivityフレームワークを使用してセッションからデータを受け取ったときに、データを更新してテーブルをリロードしたいという問題に苦労しています。ビューコントローラの瞬時にここでMultipeer Connectivity(Swift 3)を使用したセッションでdidReceiveData()によってtable.reloadDataを呼び出す方法
はMPCHandlerのdidReceiveData機能のための私のコードです:WinnerDataController
func syncWinnerFromDictionary(d: NSDictionary){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entityDescription = NSEntityDescription.entity(forEntityName: "Winner", in: context)!
let winner: Winner = NSManagedObject(entity: entityDescription, insertInto: context) as! Winner
winner.setValue(d.value(forKey: "index"), forKey: "index")
winner.setValue(d.value(forKey: "dept"), forKey: "dept")
winner.setValue(d.value(forKey: "name"), forKey: "name")
save()
appDelegate.resultViewController.tableView.reloadData()
}
での同期機能については
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
NSLog("%@", "didReceive: \(data)")
if var dict: [NSDictionary] = NSKeyedUnarchiver.unarchiveObject(with: data) as? [NSDictionary]{
let d = dict.removeFirst()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.winnerDataController.syncWinnerFromDictionary(d: dict)
}
}
私はデータの受信時にresultViewContollerのテーブルを更新し、再ロードします。しかし、appDelegate.resultViewController.tableView.reloadData()
に行くと、コンソールはfatal error: unexpectedly found nil while unwrapping an Optional value
を出します。私は一日中、tableViewが無限である理由を理解していましたが、まだ何も考えていませんでした。
誰かが助けてもらえますか? Thx
結果ビューコントローラで@Hitesh Sultaniya:NotificationCenter.default.addObserver(self, selector: #selector(testing), name: NSNotification.Name(rawValue: "syncWinner"), object: nil)
とfunc testing(notification: Notification){ print("testing") self.tableView.reloadData() }
が提供する次のコードを試しました。また、WinnerDataControllerのNotificationCenter.default.post(name: NSNotification.Name(rawValue: "syncWinner"), object: nil)
。
コンソールでは、「テスト」が表示されます。ただし、数秒後に "このアプリケーションは、メインスレッドからエンジンにアクセスした後、バックグラウンドスレッドから自動レイアウトエンジンを変更しています。これはエンジンの破損や異常なクラッシュにつながります。" が表示されます。
func testing(notification: Notification){
DispatchQueue.global(qos: .userInitiated).async{
self.tableView.reloadData()
}
}
は現在、エラーが示されていない:
そして、私はこれを試みました。しかし、表には、まだ、まだ...再読み込みされていないあなたがテーブルビューにリロードする通知オブザーバを設定することができresultViewControllerで
ご協力いただきありがとうございます。しかし、それは動作しません – lulusen
ありがとう!できます – lulusen