简体   繁体   English

Realm.write被跳过

[英]Realm.write is being skipped

I am using Realm and trying to reorder cells in my TableView. 我正在使用Realm并尝试对TableView中的单元格进行重新排序。 Behaviour that I find odd is that it works sometimes exactly like I want (it updates the data in Realm properly) but sometimes when it hits the line: 我觉得奇怪的行为是,有时它的工作方式完全符合我的要求(它正确地更新了Realm中的数据),但有时却行不通:

myRealm.write {

it does not go inside this block. 它不会进入此块。 Using breakpoints I can see it skips the whole block and goes directly to the end of this block. 使用断点,我可以看到它跳过了整个块,并直接转到该块的末尾。 Because of this, even-though it gives the visual appearance that the tableView has been re-ordered, it has not been re-ordered in Realm. 因此,尽管它提供了tableView已重新排序的视觉外观,但在Realm中尚未重新排序。

I should note that I am using a column in a table in Realm called order as a means of storing order. 我应该注意,我在Realm的表中使用一列作为命令来存储命令。 Also, labels is the array from Realm of the Table that I am trying to order 另外,标签是我要订购的表领域的数组

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 { }
        }
}

You're calling a throwing initializer Realm() in a do-catch block without any error handling at all. 您正在do-catch块中调用引发初始化器Realm() ,而根本没有任何错误处理。 I'd guess that this fails. 我猜这失败了。 Instead of catching the error and let it disappear unhandled, I'd rather recommend to use try! 我宁愿建议您try!使用try! , which would cause a runtime error here. ,这将导致运行时错误。

But then you should also ensure that you have accessed your Realm before at least once initially to avoid having to worry about failing schema migrations / validations at this point. 但是随后,您还应确保至少在开始之前至少访问过一次Realm,以避免此时担心架构迁移/验证失败。 Most errors which can still occur later with the default realm setup are typically only seen during development, eg when the Browser is attached to the simulator files. 通常在开发过程中(例如,将浏览器附加到模拟器文件时),通常只会在开发过程中看到大多数在默认领域设置之后仍会发生的大多数错误。

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

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