Swift:领域-在向数据库添加数据的同时更新UI(进度) [英] Swift: Realm - Update UI (Progress) while adding Data to DB

查看:82
本文介绍了Swift:领域-在向数据库添加数据的同时更新UI(进度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个7MB的JSON文件,然后将数据(30000个数据集)添加到领域.

I want to download a 7MB JSON-File and after that I want to add the Data (30000 Datasets) to realm.

在遍历数据集时,无法更新UI(标签或其他内容)

while looping through the Datasets it is not possible to update the UI (Label or something)

   let manager = Alamofire.SessionManager.default
    manager.session.configuration.timeoutIntervalForRequest = 20


    manager.request( "http://myURL.json")
        .downloadProgress { progress in

            self.TitelLabel.text =  "loading File :\(String(format: "%.0f", progress.fractionCompleted * 100))%"


        }
        .responseJSON { response in
            print(response.request! as Any)
            switch response.result {
            case .success:

               if let value = response.result.value {
                    self.jsonObj = JSON(value)
                    print(self.jsonObj.count)


                    for i in 0..<self.jsonbj.count{
                    self.TitelLabel.text = "..adding " + i + " article" 
                            let article = Articles()
    articles.price = self.jsonObj[i]["price"].stringValue.replacingOccurrences(of: "'", with: "´")
    article.title = self.jsonObj[i]["title"].stringValue.replacingOccurrences(of: "'", with: "´")
    article.path = self.jsonObj[i]["path"].stringValue
    article.name = self.jsonObj[i]["name"].stringValue
    article.weight = self.jsonObj[i]["weight"].stringValue

     try! realm.write {
            realm.add(article)
        }
                    }
                }

            default:
                break
            }
    }
}

如何更改显示进度百分比的标签?

What can I do to change a Label showing the Progress in percent??

推荐答案

我在这里可以看到两个问题.首先,保存到领域是在主线程上完成的,因为您需要在后台线程内移动代码.其次,领域对象是一个接一个地保存的,这不是将数据保存到磁盘上的优化方法

I can see two issues here. First, the saving to realm is done on main thread, for that you need to move the code inside the background thread. Second the realm object is saved one by one and that's not an optimised way to save data on disk

下面是可以用for循环替换的代码(带有注释).

Below is the code (with comments) that you can replace with your for loop.

// This is to do the task on background
DispatchQueue.global(qos: .background).async {
  // Moved realm.write out of for to improve the performance
  let realm = try? Realm()
  try! realm.write {
    for i in 0..<self.jsonbj.count {
      // Since this is bg thread so UI task should be done on UI thread
      DispatchQueue.main.async {
        self.TitelLabel.text = "..adding " + i + " article"
        // If you want it in percentage then use the below code
        //self.TitelLabel.text = "Adding " + (i*100.0/self.jsonbj.count) + "%"
      }
      let article = Articles()
      articles.price = self.jsonObj[i]["price"].stringValue.replacingOccurrences(of: "'", with: "´")
      article.title = self.jsonObj[i]["title"].stringValue.replacingOccurrences(of: "'", with: "´")
      article.path = self.jsonObj[i]["path"].stringValue
      article.name = self.jsonObj[i]["name"].stringValue
      article.weight = self.jsonObj[i]["weight"].stringValue

      realm.add(article)
    }
  }
}

这篇关于Swift:领域-在向数据库添加数据的同时更新UI(进度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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