Swift和Object Mapper中的Realm DB(Json)

时间:2016-08-01 06:21:05

标签: json swift realm

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

下面是代码:

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数据firsttime一直有效,我把控制台/日志放在下面详细说明我的情况:

enter image description here

但是在我进入领域DB并尝试回来之后,它似乎是null / nil对象,下面是console / log

enter image description here

{} 字符串应该是json对象...这是问题

有人可以帮我解释使用领域数据库的正确方法,或者你是否注意到我代码中的错误?感谢。

1 个答案:

答案 0 :(得分:0)

您正在尝试保存对象,然后在同一个写入事务中访问它。您应该将其保存在写入事务中,然后在事务块完成后访问它。 除此之外,您正在更改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()
    })
}

我建议你甚至不要为这个更新移动回线程,写事务不是很重,你在这里做的线程之间的上下文切换..再次取决于具体情况.. 你可以像这样保持简单:

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