简体   繁体   中英

Realm DB in Swift And Object Mapper (Json)

Halo Guys, i'm new on Swift Programming, i had a problem, i use a Realm DB to storing my data's after user has been authenticated. the result that came from server apps has been mapped to be json string, its work, and after json converted to be object / model, i put it into the realm DB,

below is the code's :

print(response.text)
                let requestResult:RequestResult = Mapper<RequestResult>().map(response.text)!
                var publicLogin : PublicLogin = Mapper<PublicLogin>().map(requestResult.result)!
                let t : String = Mapper<PublicLogin>().toJSONString(publicLogin)!
                print("TTT===== "+t)
                dispatch_sync(dispatch_queue_create("background", nil)){
                    let realm = try! Realm()
                    try! realm.write{
                        realm.add(publicLogin)
                        publicLogin = realm.objects(PublicLogin.self)[0]
                        let x : String = Mapper<PublicLogin>().toJSONString(publicLogin)!
                        print(" ########## \n : "+x)
                        let sideMenu = SSASideMenu(contentViewController: UINavigationController(rootViewController: HomeViewController()), leftMenuViewController: LeftMenuViewController(), rightMenuViewController: RightMenuViewController())
                        let color:UIColor = UIColor(netHex:0x000000)
                        sideMenu.view.backgroundColor = color
                        sideMenu.configure(SSASideMenu.MenuViewEffect(fade: true, scale: true, scaleBackground: false))
                        sideMenu.configure(SSASideMenu.ContentViewEffect(alpha: 1.0, scale: 0.7))
                        sideMenu.configure(SSASideMenu.ContentViewShadow(enabled: true, color: UIColor.blackColor(), opacity: 0.6, radius: 6.0))
                        self.presentViewController(sideMenu, animated: true, completion: nil)
                        self.loginButton.selected = false ;
                        progress.Close()
                    }
                }

i was make sure, the json data firsttime has been valid, i place the console / log below to detailing my case :

在此处输入图片说明

But after i put into realm DB and try to get back, it seems null / nil objects, below is console / log

在此处输入图片说明

The {} string should be a json object... this is the problem

Can anybody help me to explain the right way using realm DB or if you notice the mistake in my code's ? Thanks.

You are trying to save the object and then access it at the same write transaction. You should save it in a write transaction, and then access it after the transaction block is finished. Beside that, you are changing UI elements, at a back thread, and inside a write transaction, this is wrong. Like I sad, dealing with UI only on main thread.

Here is an example:

let requestResult:RequestResult = Mapper<RequestResult>().map(response.text)!
var publicLogin : PublicLogin = Mapper<PublicLogin>().map(requestResult.result)!
let t : String = Mapper<PublicLogin>().toJSONString(publicLogin)!
print("TTT===== "+t)
dispatch_sync(dispatch_queue_create("background", nil)){
    let realm = try! Realm()
    try! realm.write{
        realm.add(publicLogin)
    }
    dispatch_async(dispatch_get_main_queue(), { 
        publicLogin = realm.objects(PublicLogin.self)[0]
        let x : String = Mapper<PublicLogin>().toJSONString(publicLogin)!
        print(" ########## \n : "+x)
        let sideMenu = SSASideMenu(contentViewController: UINavigationController(rootViewController: HomeViewController()), leftMenuViewController: LeftMenuViewController(), rightMenuViewController: RightMenuViewController())
        let color:UIColor = UIColor(netHex:0x000000)
        sideMenu.view.backgroundColor = color
        sideMenu.configure(SSASideMenu.MenuViewEffect(fade: true, scale: true, scaleBackground: false))
        sideMenu.configure(SSASideMenu.ContentViewEffect(alpha: 1.0, scale: 0.7))
        sideMenu.configure(SSASideMenu.ContentViewShadow(enabled: true, color: UIColor.blackColor(), opacity: 0.6, radius: 6.0))
        self.presentViewController(sideMenu, animated: true, completion: nil)
        self.loginButton.selected = false ;
        progress.Close()
    })
}

I would recommend you to not even move to back thread for this update, write transaction isn't a very heavy compered to the contexet switch between threads you are doing here.. again it depends on the on the circumstances.. You can just keep it simple like this:

   let requestResult:RequestResult = Mapper<RequestResult>().map(response.text)!
    var publicLogin : PublicLogin = Mapper<PublicLogin>().map(requestResult.result)!
    let t : String = Mapper<PublicLogin>().toJSONString(publicLogin)!
    print("TTT===== "+t)
        let realm = try! Realm()
        try! realm.write{
            realm.add(publicLogin)
        }
    publicLogin = realm.objects(PublicLogin.self)[0]
    let x : String = Mapper<PublicLogin>().toJSONString(publicLogin)!
    print(" ########## \n : "+x)
    let sideMenu = SSASideMenu(contentViewController: UINavigationController(rootViewController: HomeViewController()), leftMenuViewController: LeftMenuViewController(), rightMenuViewController: RightMenuViewController())
    let color:UIColor = UIColor(netHex:0x000000)
    sideMenu.view.backgroundColor = color
    sideMenu.configure(SSASideMenu.MenuViewEffect(fade: true, scale: true, scaleBackground: false))
    sideMenu.configure(SSASideMenu.ContentViewEffect(alpha: 1.0, scale: 0.7))
    sideMenu.configure(SSASideMenu.ContentViewShadow(enabled: true, color: UIColor.blackColor(), opacity: 0.6, radius: 6.0))
    self.presentViewController(sideMenu, animated: true, completion: nil)
    self.loginButton.selected = false ;
    progress.Close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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