2017-02-22 3 views
0

Ich habe versucht, Realm-Datenbank einzurichten und alles hat bisher gut funktioniert. Nun, da ich eine Benachrichtigung Token zu schaffen, um versuchen Veränderungen in meinem Reich zu verfolgen, gibt addNotificationBlock Methode die folgenden Fehler:Realm addNotificationBlock gibt Fehler zurück

Error Domain=io.realm Code=1 "std::exception" UserInfo={NSLocalizedDescription=std::exception, Error Code=1} 

ich die API-Referenz gelesen und ich verstehe, dass:

This can only currently happen if opening the Realm on a background thread to calcuate the change set fails.

Leider hilft mir das nicht, den Grund herauszufinden, warum solch ein Fehler geschieht. In meiner App habe ich eine Realm-Datenbank mit 100 Objekten und ich versuche, jedes Objekt darzustellen, wo die Variable location = 2 ist. Ich möchte Benachrichtigungen über die Änderungen im Results-Objekt mit all diesen Objekten hören.

-Code in meinem 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] 
    } 

Und eine Hilfsmethode in einer anderen Datei:

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() 
    } 

Vielen Dank im Voraus.

Antwort

1

Eine unübersetzte std::exception soll nie aus Realm herauskommen, so dass dieser Teil ein Fehler ist.

Wenn Sie das Problem konsistent reproduzieren können, können Sie einen Ausnahmehaltepunkt in Xcode festlegen, um an der Stelle zu brechen, an der der Fehler tatsächlich auftritt, und hoffentlich herauszufinden, was auf diese Weise falsch ist.

+0

Vielen Dank für Ihre Antwort. Ich tat, was Sie vorgeschlagen hatten, und ein Breakpoint erschien bei 'void RealmCoordinator :: pin_version (VersionID versionid)' Methode in Zeile 'm_advancer_sg-> begin_read (versionid);' Sagt Ihnen das etwas? –

+0

Das ist definitiv ein Fehler in Realm, den du unter https://github.com/realm/realm-cocoa/issues melden solltest. –

+0

Danke @Thomas Goyne. Ich habe das Problem hier gemeldet: https://github.com/realm/realm-cocoa/issues/4693 –

Verwandte Themen