Realm object update fails on write

霸气de小男生 提交于 2019-12-25 02:44:12

问题


I am trying to update a realm object everytime there is a value change in firebase. It just like whenever there's a change of object in the server it should automatically trigger a new write and update an obejct with primary key uid.

Here is a question I posted couple days ago. It gave me a momentary solution which worked for the moment but then following error started to pop up.

  1. For the first run, everything ran fine and updated the object. But, as soon as I re-ran the program, It created a duplicate object with empty primary key.

  2. It crashes the app saying that i am trying to overwrite the primarykey object.

  3. I even got errors where it said the write transaction was already in process.

I am exactly following the Realm Documentation provided here, but totally out of luck.

The code from where I am making update triggers to the realm object:

        FBDataservice.ds.REF_USERS.child(uuid).observe(.value, with: { (snapshot) in
        if snapshot.hasChild("credentials") {
            if let snap = snapshot.value as? Dictionary<String, Any> {
                if let cred = snap["credentials"] as? Dictionary<String, Any> {
                    let u = User(userid: uuid, userData: cred)
//                        try! RealmService.uirealm.realm.write {
                        self.usercache.uid = uuid
                        self.usercache.name = u.name
                        self.usercache.bio = u.aboutme
                        if u.phoneNumer != nil {
                            self.usercache.phone = u.phoneNumer
                        }
                        self.usercache.email = u.email
                        self.usercache.username = u.username
                        if u.userPic != nil {
                            do {
                                self.usercache.imageL = try Data(contentsOf: URL(fileURLWithPath: u.userPic))
                            }catch {
                                print("The image doesnt exists")
                            }
                        }
//                            self.usercache.writeToRealm()
                    try! RealmService.uirealm.realm.write {
                        RealmService.uirealm.realm.create(UserData.self, value: ["uid":uuid], update: true)
                    }
//                            RealmService.uirealm.realm.add(self.usercache, update: true)



//                        }
                }
            }
        }
    })

My Realm Object :

class UserData : Object {

@objc dynamic var name: String? = nil
@objc dynamic var bio: String? = nil
@objc dynamic var email: String? = nil
@objc dynamic var username: String? = nil
@objc dynamic var phone: String? = nil
@objc dynamic var uid : String? = nil
@objc dynamic var imageL: Data? = nil

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

}

extension UserData {


func writeToRealm(){
    try? RealmService.uirealm.realm.write {
        RealmService.uirealm.realm.add(self, update: true)
    }
}

func DeleteFromRealm(object: Results<UserData>){
    try? RealmService.uirealm.realm.write {
        RealmService.uirealm.realm.delete(object)
    }
}

}

The Realm Definition:

class RealmService {

static let uirealm = RealmService()

private var _initRS = try! Realm()

var realm: Realm! {
    return _initRS
}
}

I am really struggling to find the correct solution to this. Any help is really appreciated. If theres any more information to be provided for this issue. Please do let me know.

来源:https://stackoverflow.com/questions/50123907/realm-object-update-fails-on-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!