簡體   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