将数据插入到进度数据库中吗? [英] Inserting data into realm DB with progress?

查看:95
本文介绍了将数据插入到进度数据库中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下载json字符串后,我有大约7MB的请求数据,这意味着json字符串大约为7MB。下载后,我想将数据保存到领域模型对象(表)中,进度如下: / p>

(1/7390)至(7390/7390)->(要插入的数据/要插入的总数据)



我在应用程序上使用Alamofire作为HTTPClient。因此,如何在从服务器下载后将进度数据插入到我的领域对象模型中? / p>

我不会准确显示数据模型,因此,任何示例都可以理解。我的json字符串为。

  {
{
名字:Smith,
年龄:23​​,
地址:纽约
},
{
名称:Adam,
年龄:22,
地址:Maimi
},
{
名称:Johnny,
年龄:33 ,
地址:拉斯维加斯
},
...约7392记录rds
}


解决方案

来自Realm的胜美。首先,Realm无法知道数据总量。因此,应在您的代码中计算进度并通知进度。



总计是对结果数组的计数。将 count 存储为本地变量。然后定义 progress 变量以存储持久数。 进度每次将对象保存到领域时都应增加。然后通知进度。

  let count = results.count //如果count>存储结果
的数量0 {
,如果让用户= results.array {
let进度= 0 //持久化的
的数量
let realm =试试! Realm()
试试realm.write {
用户中的用户{
let userList = UserList()
[...]
realm.add(userList,更新:true)

progress + = 1 //保存后,递增进度
notify(progress,total:count)
}
}
}
}

所以有几种方法可以通知进度。这里我们以 NSNotificationCenter 为例:

  func notify(progress:Int ,total:Int){
NSNotificationCenter
.defaultCenter()
.postNotificationName( ProgressNotification,object:self,userInfo:[ progress:progress, total:total])
}


I have request data which was about 7MB after it has downloaded the json string,means the json string is about 7MB.After it has downloaded,I would like to save the data into realm model object(table) with progress like

(1/7390) to (7390/7390) -> (data which is inserted/total data to be inserted)

I am using Alamofire as HTTPClient at my app.So,how to insert data with progress into my realm object model after it has downloaded from server?Any help cause I am a beginner.

I wont show the data model exactly,so,any example is appreciated.Let say my json string is.

{
    {
       name : Smith,
       age : 23,
       address : New York
    },
    {
       name : Adam,
       age : 22,
       address : Maimi
    },
    {
       name : Johnny,
       age : 33,
       address : Las Vegas
    },
    ... about 7392 records
}

解决方案

Katsumi from Realm here. First, Realm has no way to know the total amount of data. So calculating progress and notifying it should be done in your code.

A total is a count of results array. Store count as a local variable. Then define progress variable to store the number of persisted. progress should be incremented every save an object to the Realm. Then notify the progress.

let count = results.count // Store count of results
if count > 0{
    if let users = results.array {
        let progress = 0 // Number of persisted

        let realm = try! Realm()
        try realm.write {
            for user in users{
                let userList=UserList()
                [...]
                realm.add(userList,update: true)

                progress += 1 // After save, increment progress
                notify(progress, total: count)
            }
        }
    }
}

So there are several ways to notify the progress. Here we use NSNotificationCenter for example:

func notify(progress: Int, total: Int) {
    NSNotificationCenter
        .defaultCenter()
        .postNotificationName("ProgressNotification", object: self, userInfo: ["progress": progress, "total": total])
}

这篇关于将数据插入到进度数据库中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆