繁体   English   中英

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

[英]Realm DB in Swift And Object Mapper (Json)

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

下面是代码:

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

我确定json数据首次有效,我将控制台/日志放在下面以详细说明我的情况:

在此处输入图片说明

但是在我进入领域数据库并尝试返回之后,似乎是null / nil对象,下面是console / log

在此处输入图片说明

{}字符串应该是一个json对象...这就是问题所在

有人可以帮助我解释使用领域数据库的正确方法吗?或者您发现我的代码中的错误吗? 谢谢。

您正在尝试保存对象,然后在同一写入事务中对其进行访问。 您应将其保存在写事务中,然后在事务块完成后对其进行访问。 除此之外,您还需要在后台线程和写事务内部更改UI元素,这是错误的。 就像我难过一样,只在主线程上处理UI。

这是一个例子:

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

我建议您不要在此更新中使用后退线程,写入事务对于在此处执行的线程之间的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