Label和ProgressBar不会更新macOS Swift

时间:2018-03-23 17:48:05

标签: swift xcode macos label progress-bar

我今天的代码有问题。我尝试更新for循环中的标签和进度视图,但标签或进度条都没有更新。

我无法理解为什么......如果你可以处理它,这是代码

    func parseMessage()
    {
        var i = 0

        self.beginHTML()

        for row in (try? db?.prepare("SELECT message.text, message.is_from_me, datetime(substr(message.date, 1, 9) + 978307200, 'unixepoch', 'localtime') as f_date, message.cache_has_attachments, message.ROWID FROM message LEFT JOIN chat WHERE chat.guid LIKE '%PHONENUMBER%' AND chat.ROWID == message.handle_id"))!!
        {
            html += """
            <div class=\"clear\"></div>
            <p align=\"center\"> \(String(describing: row[2] ?? "")) </p>
            """

            if (row[3] as! Int64 == 1)
            {
                self.parseAttachments(row: row)
            }
            else if (row[1] as! Int64 == 1)
            {
                html += "<div class=\"from-me\"> <p>\(String(describing: row[0] ?? "")) </p></div>"
            }
            else
            {
                html += "<div class=\"from-them\"> <p>\(String(describing: row[0] ?? "")) </p></div>"
            }

            i = i + 1

            if (i % 500 == 0)
            {
                self.progressLabel.stringValue = "\(i) on \(self.messageNumber)"
                self.progressBar.increment(by: 500)

//                DispatchQueue.main.async {
//                    self.progressLabel.stringValue = "\(i) on \(self.messageNumber)"
//                    self.progressBar.increment(by: 500)
//                }
            }

            if (i == 2000)
            {
                break;
            }
        }

        html += "</section></body></html>"
    }

我的整个代码在这里:https://github.com/ExPl0siF/iOSMessageExport/blob/master/MessageGenerator/ViewController.swift

1 个答案:

答案 0 :(得分:2)

您无法在主线程上运行for循环并显示UI更新。 UI更改不会发生,直到您的代码返回并且应用程序为主事件循环提供服务。

相反,如果您发布的代码是在后台线程上运行(可能应该是这样),那么您需要在调用DispatchQueue.main.async()时包装UI更新(更改标签和进度条)

编辑:尝试此更改:

func parseMessage()
{
    DispatchQueue.global().async {
        var i = 0

        self.beginHTML()

        for row in (try? db?.prepare("SELECT message.text, message.is_from_me, datetime(substr(message.date, 1, 9) + 978307200, 'unixepoch', 'localtime') as f_date, message.cache_has_attachments, message.ROWID FROM message LEFT JOIN chat WHERE chat.guid LIKE '%PHONENUMBER%' AND chat.ROWID == message.handle_id"))!!
        {
            html += """
            <div class=\"clear\"></div>
            <p align=\"center\"> \(String(describing: row[2] ?? "")) </p>
            """

            if (row[3] as! Int64 == 1)
            {
                self.parseAttachments(row: row)
            }
            else if (row[1] as! Int64 == 1)
            {
                html += "<div class=\"from-me\"> <p>\(String(describing: row[0] ?? "")) </p></div>"
            }
            else
            {
                html += "<div class=\"from-them\"> <p>\(String(describing: row[0] ?? "")) </p></div>"
            }

            i = i + 1

            if (i % 500 == 0)
            {
                DispatchQueue.main.async {
                    self.progressLabel.stringValue = "\(i) on \(self.messageNumber)"
                    self.progressBar.increment(by: 500)
                }
            }

            if (i == 2000)
            {
                break;
            }
        }

        html += "</section></body></html>"
    }
}
相关问题