【问题标题】:Swift: Realm accessed from incorrect threadSwift:从不正确的线程访问的领域
【发布时间】:2016-07-11 07:44:42
【问题描述】:

我从以前的视图控制器传递它。我想显示我保存在其他视图控制器中的数据。

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "Beacons") {


        let nav = segue.destinationViewController as! UINavigationController
        let svc = nav.topViewController as! BeaconCharacteristicViewController
        svc.selectedList = sender as! Beacons


    }

      var selectedList : Beacons!

这个代码由于某种原因出错。

我不使用主键,或者我必须使用它。

    func loadproperties(){

      let beacons = selectedList

      let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT

      dispatch_async(dispatch_get_global_queue(priority, 0)) {

        do{
            try uiRealm.write({ () -> Void in

                beacons.uuid = self.beaconUUID.text!
                print("uuid = \(beacons.uuid)")

                beacons.major = self.beaconMajor.text!
                print("uuid = \(beacons.uuid)")

                beacons.minor = self.beaonMinor.text!
                print("uuid = \(beacons.uuid)")

                uiRealm.add(beacons, update: true)

                }
            )
        } catch {
            print("error")
        }

       }
    }

错误

       Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread.'

谢谢你:)))

【问题讨论】:

    标签: ios swift realm


    【解决方案1】:

    根据您的代码try uiRealm.write,您似乎在主线程(调度块)中调用了另一个线程的领域实例。尝试创建另一个实例。

        do{
            // create new instance
            let uiRealm = try! Realm()
    
            try uiRealm.write({ () -> Void in
    
                beacons.uuid = self.beaconUUID.text!
                print("uuid = \(beacons.uuid)")
    
                beacons.major = self.beaconMajor.text!
                print("uuid = \(beacons.uuid)")
    
                beacons.minor = self.beaonMinor.text!
                print("uuid = \(beacons.uuid)")
    
                uiRealm.add(beacons, update: true)
    
                }
            )
        } catch {
            print("error")
        }
    

    【讨论】:

    • 谢谢,但它会导致这个错误“realm::IncorrectThreadException: Realm accessed from wrong thread.”
    • 您是否尝试删除调度块?
    • 没关系,但是错误信标'没有主键,无法更新'。顺便说一句,我会尝试找到解决方案。谢谢你的帮助:))
    • 一般我加主键,更容易得到对象。
    • 我会做主键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多