【问题标题】:Dispatchqueue and SVProgressHUD in swift快速调度队列和 SVProgressHUD
【发布时间】:2019-02-22 03:37:31
【问题描述】:

我一直在设计一个分析文本行的应用程序,我想使用 SVProgressHUD 来显示它的进度。

这是我的代码:

let total = text.count
for line in text{
    count = count + 1
    DispatchQueue.global(pos: .background).async{
        //Analyzing line
        DispatchQueue.main.async{
            SVProgressHUD.showProgress(count/total)
        }
    }
}

分析成功,HUD显示正常,当count达到total时,进程卡住,SVProgressHUD停止在max状态,程序停止。程序有什么问题?

我使用 Dispatchqueue 是不是错了? 我是否必须调用其他东西来释放后台进程或其他东西?

我试过交换//Analyzing lineSVProgressHUD.show...,但还是不行。我最初在没有 dispatchqueue 的循环中使用 SVProgress,但只有在分析(完整循环)完成后,进度 hud 才会移动,这是一个问题。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 达到 100% 后会发生什么?
  • @RakeshaShastri 应该跳出循环,移除 ProgressHUD。

标签: swift svprogresshud dispatch-queue


【解决方案1】:

尝试使用此代码。它不使用循环,而是实现对用于处理字符串数据的函数的递归调用。

func processLines() {
    SVProgressHUD.showProgress(0)

    // sample data
    var text = Array("abcdefghijklmnop")
    var lines : [Line] = []
    let count : [Int] = Array(0..<text.count)
    count.forEach({ pos in lines.append(Line(char: text[pos])) })
    var currentIndex : Int = 0

    func processLine(at index: Int) {

        DispatchQueue.global(qos: .background).async{
            //Analyzing line
            let line = lines[index]
            print("Processing Line CHAR: \(line.char)")

            DispatchQueue.main.async{
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    guard currentIndex < lines.count-1 else {
                        SVProgressHUD.showProgress(1)
                        return
                    }
                    currentIndex += 1
                    startLineProces(at: currentIndex)
                }
            }
        }

    }

    func startLineProces(at index: Int) {
        processLine(at: index)
        SVProgressHUD.showProgress(Float(index) / Float(lines.count))
    }


    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        startLineProces(at: currentIndex)
    }
}

struct Line {
   var char: Character
   init(char: Character) {
     self.char = char
   }
}

【讨论】:

    【解决方案2】:

    使用以下字符串扩展名来分析您的字符串。它有一个完成块,它将返回进度以及完成状态。

    extension String {
    func analyseString(completion: @escaping (Bool, Float) -> ()) {
        let totalCountOfString = self.count
        for (index, _) in self.enumerated() {
            if index == totalCountOfString - 1 {
                completion(true, Float(index)/Float(totalCountOfString))
            } else {
                completion(false, Float(index)/Float(totalCountOfString))
            }
        }
      }
    }
    

    您可以调用上述方法来显示您的进度,如下所示(可能是单击按钮)。 self.yourString 是您需要分析的输入字符串。

    @IBAction func clicked(_ sender: UIButton) {
        DispatchQueue.main.async {
            self.yourString.analyseString { (isCompleted, progress) in
                if isCompleted {
                    SVProgressHUD.dismiss()
                    print("Ending")
                } else {
                    SVProgressHUD.showProgress(progress, status: "Analysing (\(progress)%)")
                }
            }
        }
    }
    

    【讨论】:

    • 或者在你的代码中试试这个:SVProgressHUD.showProgress(Float(count)/Float(total))
    • 我试过你的代码,计算和HUD显示代码一起工作(我试过放打印语句),但是直到计算完成才出现HUD进度。 SVProgressHUD.showProgress 行运行,但实际上并未显示。
    • @Mininny 尝试输入大字符串(可能 1000 字符长)以查看可区分的进度。
    • 我将它与包含约 10K 行字符串的文本一起使用,这些字符串很长。而且我认为后台处理有问题,但我不确定为什么 HUD 不会出现。
    猜你喜欢
    • 1970-01-01
    • 2017-10-26
    • 2011-09-01
    • 1970-01-01
    • 2017-05-06
    • 2011-01-19
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多