【发布时间】: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 个事务,我相信这会提高性能。
-
你有什么问题?