繁体   English   中英

Realm.write被跳过

[英]Realm.write is being skipped

我正在使用Realm并尝试对TableView中的单元格进行重新排序。 我觉得奇怪的行为是,有时它的工作方式完全符合我的要求(它正确地更新了Realm中的数据),但有时却行不通:

myRealm.write {

它不会进入此块。 使用断点,我可以看到它跳过了整个块,并直接转到该块的末尾。 因此,尽管它提供了tableView已重新排序的视觉外观,但在Realm中尚未重新排序。

我应该注意,我在Realm的表中使用一列作为命令来存储命令。 另外,标签是我要订购的表领域的数组

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

        //update when you move a row up
        if sourceIndexPath.row > destinationIndexPath.row {
            let lowerbound = destinationIndexPath.row
            let upperbound = sourceIndexPath.row
            do{
                let myRealm = try Realm()

                myRealm.write {

                    let labelAtSource = self.labels![upperbound]
                    labelAtSource.order = lowerbound

                    for i in lowerbound...upperbound-1{
                        let label = self.labels![i]
                        label.order = i+1
                    }
                }   
            }catch { }
        }else{ //when you move a row down
            let lowerbound = sourceIndexPath.row
            let upperbound = destinationIndexPath.row
            do{
                let myRealm = try Realm()

                myRealm.write {

                    let labelAtSource = self.labels![lowerbound]
                    labelAtSource.order = upperbound

                    for i in lowerbound+1...upperbound{
                        let label = self.labels![i]
                        label.order = i-1
                    }
                }
            }catch { }
        }
}

您正在do-catch块中调用引发初始化器Realm() ,而根本没有任何错误处理。 我猜这失败了。 我宁愿建议您try!使用try! ,这将导致运行时错误。

但是随后,您还应确保至少在开始之前至少访问过一次Realm,以避免此时担心架构迁移/验证失败。 通常在开发过程中(例如,将浏览器附加到模拟器文件时),通常只会在开发过程中看到大多数在默认领域设置之后仍会发生的大多数错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM