Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 领域addNotificationBlock返回错误_Ios_Swift_Realm - Fatal编程技术网

Ios 领域addNotificationBlock返回错误

Ios 领域addNotificationBlock返回错误,ios,swift,realm,Ios,Swift,Realm,我一直在尝试设置领域数据库,到目前为止一切都很好。现在,我正在尝试创建通知令牌以跟踪领域中的更改,addNotificationBlock方法返回以下错误: Error Domain=io.realm Code=1 "std::exception" UserInfo={NSLocalizedDescription=std::exception, Error Code=1} 我已阅读API参考资料,并了解: 当前只有在后台打开领域时才能发生这种情况 计算更改集的线程失败 不幸的是,这并不能帮助我

我一直在尝试设置领域数据库,到目前为止一切都很好。现在,我正在尝试创建通知令牌以跟踪领域中的更改,addNotificationBlock方法返回以下错误:

Error Domain=io.realm Code=1 "std::exception" UserInfo={NSLocalizedDescription=std::exception, Error Code=1}
我已阅读API参考资料,并了解:

当前只有在后台打开领域时才能发生这种情况 计算更改集的线程失败

不幸的是,这并不能帮助我找出失败的原因。在我的应用程序中,我有一个包含100个对象的领域数据库,我试图显示变量location=2的每个对象。我想听听关于包含所有这些对象的Results对象中的更改的通知

我的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]
    }

提前谢谢。

一个未翻译的
std::exception
永远不会超出范围,所以这部分是一个bug


如果您能够始终如一地重现该问题,您可以在Xcode中设置一个异常断点,使其在实际发生错误的地方中断,并希望通过这种方式找出问题所在。

谢谢您的回复。我按照你的建议做了,一个断点出现在
void RealmCoordinator::pin_version(VersionID VersionID)
method-on-line
m_advancer\u sg->begin_read(VersionID)这对你有什么启示吗?这肯定是领域内的一个bug,你应该向它报告。谢谢@Thomas Goyne。我在这里报告了这个问题:
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()
    }