簡體   English   中英

嘗試使用Realm時,App崩潰SIGABRT

[英]App Crashes SIGABRT when trying to use Realm

我正在嘗試使用Realm保存一個簡單的對象,但是即使嘗試將其包裝在Do Catch塊中,該應用仍會在嘗試進行寫事務時崩潰。

let theme = Theme()
    theme.name = "Custom Theme"
    theme.backgroundColor = backgroundColor
    theme.accentColor = accentColor
    theme.numberColor = numColor
    theme.functionColor = funcColor

    // Add to the Realm inside a transaction
    do {
        try Realm().write {
            do {
                try Realm().add(theme, update: true)
            } catch {
                print("Error saving data")
            }
        }
    } catch {
        print("Realm.write error")
    }

這是對象“主題”

class Theme : Object {
dynamic var name = ""
dynamic var backgroundColor = ""
dynamic var accentColor = ""
dynamic var numberColor = ""
dynamic var functionColor = ""

override static func primaryKey() -> String? {
    return "name"
}

}

這是崩潰SIGABRT崩潰的屏幕截圖

編輯:導致崩潰的上述代碼僅在單擊按鈕時執行。 也沒有控制台輸出。 我通過CocoaPods帶入了領域。

嗯,這可能與創建領域實例的方式有關,請嘗試以下操作:

let realm = try! Realm()

do {
    try realm.write {
        do {
            try realm.add(theme, update: true)
        } catch {
            print("Error saving data")
        }
    }
} catch {
    print("Realm.write error")
}

雖然,通常您不需要將事務包裝到do-catch塊中:

let realm = try! Realm()

try! realm.write {
    realm.add(theme, update: true)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM