【问题标题】:Swift: Realm - Update UI (Progress) while adding Data to DBSwift:Realm - 在向数据库添加数据时更新 UI(进度)
【发布时间】:2017-10-25 08:00:58
【问题描述】:

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

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

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

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

【问题讨论】:

  • 首先您可以在 30 个事务中添加 30000 个项目而不是 30000 个事务,我相信这会提高性能。
  • 你有什么问题?

标签: swift realm progress


【解决方案1】:

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

以下是您可以用 for 循环替换的代码(带有 cmets)。

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

【讨论】:

  • 我试过了,但结果是这样 *** 由于未捕获的异常“RLMException”而终止应用程序,原因:“从不正确的线程访问的领域。”
  • 那么我猜你需要在调度块中创建领域对象。更新代码请尝试
  • 对不起,它真的不起作用..标签将值更改一一两次,然后冻结..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多