异步我想工作,在后台异步从JSON写的境界,但我不明白为什么我的代码是不是必须工作。境界在后台
override func viewDidLoad() {
super.viewDidLoad()
myFunc()
}
和myFunc的():
func myFunc() {
let realm = try! Realm()
// get file JSON from local device and write data from it to RealmDB
if realm.isEmpty {
//local file JSON
let file = Bundle.main.path(forResource: "file", ofType: "json")!
let url = URL(fileURLWithPath: file)
let jsonData = NSData(contentsOf: url)!
//Serialization JSON
let json = try! JSONSerialization.jsonObject(with: jsonData as Data, options: [])
DispatchQueue.main.async {
realm.beginWrite()
//Create data from JSON to our objects
realm.create(DataRoot.self, value: json, update: true)
try! realm.commitWrite()
}
}
}
DB创造,但下一步(因为我看到)是错误的:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (transport.filter("id == 1").first?.routes.count)!
}
所以,我想等待在后台完整写入数据库(例如显示进度视图)并转到下一步。