2017-02-22 10 views
0

私はRealmデータベースをセットアップしようとしていて、これまでのところうまくいっています。今、私は私のレルムの変化を追跡するために、通知トークンを作成しようとしていますことを、addNotificationBlock方法は、次のエラーが返されます。Realm addNotificationBlock returned error

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個のオブジェクトからなるレルムデータベースがあり、可変ロケーション= 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] 
    } 

、別のファイル内のヘルパーメソッド:

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

は、事前にありがとうございます。

答えて

1

翻訳されていないstd::exceptionは決してレルムから外すことはないので、その部分はバグです。

問題を一貫して再現できる場合は、エラーが実際に発生している箇所でブレークするようにXcodeに例外ブレークポイントを設定し、何が間違っているかを見つけてください。

+0

ありがとうございます。私はあなたが提案したように、 'm_advancer_sg-> begin_read(versionid);行の' RealmCoordinator :: pin_version(VersionID versionid) 'メソッドにブレークポイントが現れました;'これはあなたに何かを言いますか? –

+0

これは間違いなくRealmのバグです。https://github.com/realm/realm-cocoa/issuesで報告してください。 –

+0

ありがとうございます@トーマスGoyne。私は問題をここに報告した:https://github.com/realm/realm-cocoa/issues/4693 –