我一直在尝试设置 Realm 数据库,到目前为止一切正常。现在我正在尝试创建通知 token 以跟踪我的 Realm 中的更改,addNotificationBlock 方法返回以下错误:
Error Domain=io.realm Code=1 "std::exception" UserInfo={NSLocalizedDescription=std::exception, Error Code=1}
我已阅读 API 引用资料并且我确实理解:
This can only currently happen if opening the Realm on a background thread to calcuate the change set fails.
不幸的是,这并不能帮助我找出发生这种故障的原因。在我的应用程序中,我有一个包含 100 个对象的 Realm 数据库,我试图在变量位置 = 2 的位置呈现每个对象。我想听取有关包含所有这些对象的结果对象的更改的通知。
我的 ViewController 中的代码:
import UIKit
import RealmSwift
class PatientCell: UITableViewCell {
@IBOutlet weak var hetu: UITextView!
@IBOutlet weak var name: UITextView!
@IBOutlet weak var photo: UITextView!
}
class PäivystyslistaVC: UIViewController, UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource, UIPopoverControllerDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet var patientPopupView: UIView!
var patients: Results<Patient2>!
var realm = try! Realm()
var timer: Timer!
var notificationToken: NotificationToken? = nil
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
let realm = try! Realm()
patients = realm.objects(Patient2.self).filter("location = 2")
print("Patients on the ER: \(patients)")
notificationToken = patients.addNotificationBlock { (changes: RealmCollectionChange) in
switch changes {
case .initial:
print("From initial")
break
case .update:
print("From update")
break
case .error(let err):
print("Error occured \(err)")
break
}
}
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.addPatient), userInfo: nil, repeats: true)
}
func addPatient() {
print("")
print("Timer launched")
print("Patients on the ER: \(patients.count)")
sendPatientToER()
print("")
tableView.reloadData()
}
// MARK: TableView:n hallinta
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return patients.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "patient", for: indexPath) as! PatientCell
let patient = patients[indexPath.row]
cell.hetu?.text = patient.hetu
cell.name?.text = patient.fullName
cell.photo?.text = patient.photo
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let potilastiedotVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PotilastiedotVC")
self.present(potilastiedotVC, animated: true, completion: nil)
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
let kotiuta = UITableViewRowAction(style: .normal, title: "Kotiuta") { action, index in
try! self.realm.write {
self.realm.create(Patient2.self, value: ["id": index.row, "location": 1], update: true)
}
self.tableView.deleteRows(at: [index], with: .left)
}
kotiuta.backgroundColor = UIColor.vihreä
let osastolle = UITableViewRowAction(style: .normal, title: "Osastolle") { action, index in
try! self.realm.write {
self.realm.create(Patient2.self, value: ["id": index.row, "location": 3], update: true)
}
self.tableView.deleteRows(at: [index], with: .top)
}
osastolle.backgroundColor = UIColor.oranssi
let lähetä = UITableViewRowAction(style: .normal, title: "Lähetä") { action, index in
try! self.realm.write {
self.realm.create(Patient2.self, value: ["id": index.row, "location": 3], update: true)
}
self.tableView.deleteRows(at: [index], with: .top)
}
lähetä.backgroundColor = UIColor.vaaleansininen
return [kotiuta, lähetä, osastolle]
}
还有另一个文件中的辅助方法:
import UIKit
import RealmSwift
func sendPatientToER() {
let realm = try! Realm()
let count = realm.objects(Patient2.self).filter("location == 1").count
print("Count of patients waiting at home: \(count)")
let randomId = Int(arc4random_uniform(UInt32(count)))
print("Random id generated: \(randomId)")
realm.beginWrite()
realm.create(Patient2.self, value: ["id": randomId, "location": 2], update: true)
try! realm.commitWrite()
}
提前谢谢你。
最佳答案
一个未翻译的 std::exception
永远不应该让它脱离 Realm,所以那部分是一个错误。
如果您能够始终如一地重现问题,则可以在 Xcode 中设置一个异常断点,以在实际发生错误的位置中断,并希望通过这种方式找出问题所在。
关于ios - Realm addNotificationBlock 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42396590/