【问题标题】:How to update progressView when downloading something in realm在领域中下载内容时如何更新progressView
【发布时间】:2019-02-12 02:24:31
【问题描述】:

我正在尝试弄清楚如何使用 progressView 来显示使用 Realm 下载的内容的进度。通过阅读似乎需要找到一个名为progress的Float,但我不知道如何在Realm中找到它。我查看了以下问题,但我认为它们在这种情况下没有太大帮助: Swift: Realm - Update UI (Progress) while adding Data to DB iOS-Swift How to update progressView

这是我目前拥有的: 视图控制器:

import UIKit
import Realm
import RealmSwift

class ViewController: UIViewController {

    var data = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."


    var realm: Realm!



    @IBOutlet weak var saveBtn: UIButton!

    @IBOutlet weak var progressView: UIProgressView!


    override func viewDidLoad() {
        super.viewDidLoad()
        print("Hello")
        print(Realm.Configuration.defaultConfiguration.fileURL!)
        realm = try! Realm()
    }

    @IBAction func saveBtnPressed(_ sender: Any) {
        saveToRealm(id: 1, name: "One", data: data)
        let progress: Float = 0
        progressView.setProgress(progress, animated: true)
    }


    func saveToRealm(id: Int, name: String, data: String) {
        let realmSave = RealmSave()
        realmSave.id = id
        realmSave.name = name
        realmSave.data = data
        try? realm!.write {
            realm.add(realmSave, update: true)
        }

    }
}

如果有什么我可以帮忙的,请询问。谢谢

【问题讨论】:

  • 那么发生了什么?加载指示器没有从屏幕上移除吗?
  • 我猜这是关于保存数据而不是从领域数据库中获取数据??

标签: ios swift realm progress-bar


【解决方案1】:

首先:定义值为 0 的 let 常量将始终保持为 0。您无法动态更改它以反映下载状态。

正如一位 Realm 工程师在 this 帖子中指出的那样“Realm 无法知道数据总量。” 估计进度应该在您的代码中完成。

你可以试试这样的:

    func estimateProgress() {

        let dataString = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."

        let data = dataString.data(using: .utf8)
        let count = Float((data?.count)!)

        let percentage: Float = count / 100

        var progress: Float = 0

        if progress < count {
           progress += percentage
        } else {
           progress = count
        }

        progressView.setProgress(progress, animated: true)
        print(progress)

}

请注意,此解决方案用于更新 UI 并让用户知道正在保存到 Realm。由于它使用数据中的字节数,因此数据越大,进度条填充所需的时间就越长。所以它对 UI 有好处,但它不是实际的进度状态。

如果您觉得进度条填充到慢/快,您可以随时调整百分比。

【讨论】:

  • @Idem。谢谢你,这工作得很好。我也知道这没关系,但我确实知道进度为 0。那只是当时的占位符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多