【问题标题】:RealmSwift - begin/commit writesRealmSwift - 开始/提交写入
【发布时间】:2016-11-03 13:42:31
【问题描述】:

在我的健身追踪器应用程序中,我保存当前跑步并调用反向地理定位方法,该方法应该获取跑步发生的位置名称。

首先我将“latestRun”写入领域,然后等待反向地理编码完成并尝试修改“latestRun”变量

try! realm.write {
        realm.add(latestRun)
    }

if !savedLocations.isEmpty
    {
         ReverseGeocoder.getPlace(latitude: savedLocations[0].latitude, longitude: savedLocations[0].longitude, completionHandler: {
            place in
            realm.beginWrite()
            try! realm.write {
                latestRun.place = place

            }
            try! realm.commitWrite()

        })
    }

结果异常

由于未捕获的异常“RLMException”而终止应用程序,原因:“The Realm 已经处于写入事务中'

我做错了什么?我在文档中发现,在这种情况下应该使用 beginWrite()commitWrite() 方法。

【问题讨论】:

    标签: ios swift swift3 realm cllocation


    【解决方案1】:

    既不需要使用commitWrite() 也不需要使用beginWrite()

    try! realm.write {
       latestRun.place = place
     }
    

    足够了

    【讨论】:

      【解决方案2】:

      是因为线

      realm.beginWrite()
      

      就在之前

      try! realm.write {
      

      他们都做同样的事情,打开一个“写作会议”。你应该只保留一个。

      注意:如果您删除 beginWrite 行并决定使用闭包变体,请不要忘记也删除该行

      try! realm.commitWrite()
      

      【讨论】:

      • 现在我有`致命错误:'试试!'表达式意外引发错误:Error Domain=io.realm Code=1 "Can't commit a non-existing write transaction" `
      • 那是因为你离开了try! realm.commitWrite()。你也应该删除它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 2010-11-14
      • 2011-09-22
      • 1970-01-01
      相关资源
      最近更新 更多