简体   繁体   English

Swift和对象映射器(Json)中的领域数据库

[英]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. Halo Guys,我是Swift编程的新手,我遇到了问题,我使用了Realm DB来存储用户通过身份验证后存储的数据。 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, 来自服务器应用程序的结果已被映射为json字符串,其工作,并且在将json转换为对象/模型后,我将其放入领域数据库中,

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 : 我确定json数据首次有效,我将控制台/日志放在下面以详细说明我的情况:

在此处输入图片说明

But after i put into realm DB and try to get back, it seems null / nil objects, below is console / log 但是在我进入领域数据库并尝试返回之后,似乎是null / nil对象,下面是console / log

在此处输入图片说明

The {} string should be a json object... this is the problem {}字符串应该是一个json对象...这就是问题所在

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. 除此之外,您还需要在后台线程和写事务内部更改UI元素,这是错误的。 Like I sad, dealing with UI only on main thread. 就像我难过一样,只在主线程上处理UI。

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: 我建议您不要在此更新中使用后退线程,写入事务对于在此处执行的线程之间的contexet切换来说不是一个很沉重的负担。同样,这取决于具体情况。像这样简单:

   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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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