Swift,Label在回调函数中没有刷新

时间:2015-07-02 08:16:05

标签: swift uilabel dispatch-async

当我在循环中运行时,我想在活动指示器上更改/重新加载我的标签。 但它没有重新加载,因为我认为“self.netUtil.callWebservice(urlStr){(data)”。

我的代码如下 -------------

     if netUtil.isConnectedToNetwork() == true
    {
        self.showActivityIndicator(self.view,message: "Synch is progress...")

            //Logic toc all Web Service
            var urlStr: String = "\(constants.SERVER_URL)"
            self.container.addSubview(self.lblProcess)
            self.netUtil.callWebservice(urlStr) {(data) -> Void in

            self.fileMgr.createFileDirectory("/Surveyor")
            self.filePath = self.fileMgr.createFile(self.fileMgr.getSurveyourPropListXmlName(""))

            self.fileMgr.writeFile(data, filenamePath: self.fileMgr.getSurveyourPropListXmlName(""))
            self.xmlNSData = data.dataUsingEncoding(NSUTF8StringEncoding)!

            self.beginParsing()
            self.loadPropertyMap()
            var unitCount = 0


                for Prop: Property in self.properties
                {

                    unitCount++

                    for view in self.view.subviews
                    {
                        if view is UILabel
                        {
                            view.removeFromSuperview()
                        }
                    }

                    /* refresh label code start here */
                     dispatch_async(dispatch_get_main_queue(), {
                        //Run UI Updates
                         self.unitCount++
                         println("unitCount \( self.unitCount)")
                        var totCount = self.properties.count
                        var message = "Synchronization \(self.unitCount)/\(totCount) buildings completed"
                        self.lblProcess.text = message
                        self.lblProcess.setNeedsDisplay()

                    })

                    /* refresh label code End here */

                    //Backup code to be shfted while pushing in web service
                    self.fileMgr.getSurveyorUnitFiles("", proCode: Prop.proCode as String, isCreateBackup: true)

                    //back up code ended
                    self.callUnitsWebservice(Prop.proCode as String)  //another web service cal

                }
             self.lblProcess.removeFromSuperview()

            self.hideActivityIndicator()
        }

附加println的输出

属性计数> 10 unitCount 1 unitCount 2 unitCount 3 unitCount 4 unitCount 5 unitCount 6 unitCount 7 unitCount 8 unitCount 9 unitCount 10

如果我将标签放在循环中而不是重新加载...   我该怎么做才能在循环中重新加载标签,以便在每个循环被激发时它会发生变化?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您只能从主线程刷新UI。

self.netUtil.callWebservice(urlStr) {(data) -> Void in}作为一个块运行,它不在主线程中。

你必须在街区内做这样的事情:

        //Logic synchronization all Web Service
        var urlStr: String = "\(constants.SERVER_URL)"

        //Add the view. Don't forget to remove it when the looping is done.
        self.container.addSubview(self.lblProcess)

        self.netUtil.callWebservice(urlStr) {(data) -> Void in    //I suspect this method to reason why my label is not refreshing

        self.beginParsing()
        var unitCount = 0

        for Prop: Property in self.properties
        {

             unitCount++

            for view in self.view.subviews
            {
                if view is UILabel
                {
                    view.removeFromSuperview()
                }
            }

            /* refresh label code start here */
            dispatch_async(dispatch_get_main_queue(), {
                    //Run UI Updates
                    var totCount = self.properties.count
                    var message = "Synchronization \(unitCount)/\(totCount) buildings completed"
                    self.lblProcess.text = message
                    self.lblProcess.setNeedsDisplay()
            })
            /* refresh label code End here */

            //Backup code to be shfted while pushing in web service
            self.fileMgr.getSurveyorUnitFiles("", proCode: Prop.proCode as String, isCreateBackup: true)

            //back up code ended
            self.callUnitsWebservice(Prop.proCode as String)  //another web service cal

        }

        self.hideActivityIndicator()
    }