0
我似乎無法將數據保存到Swift中的領域!無法將數據保存到領域
在我的班級之一,我有以下幾點:
func getDeviceIdAndPersist(){
print("getDeviceIdAndPersist")
let realm = try! Realm()
let realmWallet = RealmWallet()
let realmResults = realm.objects(RealmWallet.self)
for results in realmResults {
guard results.deviceId != nil else {
realmWallet.deviceId = NSUUID().UUIDString
try! realm.write() {
realm.add(realmWallet, update: true)
}
return
}
}
}
,它似乎不保存數據!在我的應用程序和其保存數據中,我有類似於此的代碼,但這不是!
另外我在嘗試保留數據時遇到問題,在啓動應用程序後,數據使用領域(我可以使用領域瀏覽器進行驗證)存儲。但是,一旦我重新啓動應用程序,數據不再存在於領域!有沒有人有任何建議,爲什麼數據不會被持續?
EDIT 1
這是我的境界類
class RealmWallet: Object {
dynamic var publicKey = NSData()
dynamic var preferredAddress = ""
dynamic var id = 0
let walletAddresses = List<Addresses>()
dynamic var fee: NSNumber = 0.0
dynamic var deviceId: String? = nil
dynamic var tempPreTax: String? = nil
dynamic var portalToken: String? = nil
let walletHasBackup = RealmOptional<Bool>()
let alreadyAskedForBluetooth = RealmOptional<Bool>()
let insecureMailSent = RealmOptional<Bool>()
let cameraAccessGranted = RealmOptional<Bool>()
let idNowSuccess = RealmOptional<Bool>()
dynamic var marketInfos = NSData()
override class func primaryKey() -> String? {
return "id"
}
}
編輯2
的NSUUID不是其本人不能保存的唯一數據,以下是另一個例子的數據,我似乎無法使用Realm保存:
onboardingModal!.bluetoothPressed = {() -> Void in
print("after onboardingModal")
self.cbManager = CBCentralManager(delegate: self, queue: nil)
print("REALM")
let realm = try! Realm()
do {
try! realm.write {
print("IN WRITE")
self.realmWallet.alreadyAskedForBluetooth.value = true
realm.add(self.realmWallet, update: true)
}
}
}
請添加您的'RealmWallet'代碼類,或許類有'dynamic'其變量 – xmhafiz
添加了類,它呢!它應該沒有動態嗎? – tobyapp
您是否修改默認配置('Realm.Configuration.defaultConfiguration')?此外,您是否有應用中的任何代碼來刪除磁盤上的文件,例如在啓動? – AustinZ