Search code examples
xcodeswiftuiimageviewwikipedialag

Delayed response when changing label text (Swift)


My issue is that when imageFound == false, it prints out the "No Results!!!" immediately, but it takes like 15 seconds for the labels text to change. I do not know why this is lagging but I need help trying i=to put it down into the range of 5 seconds or less.

Code is below...

if let textFieldContent = textField.text{
        do {

            try WikiFaceRec.faceForPerson(textFieldContent, size: CGSize(width: 200, height: 250), completion: {(image:UIImage?, imageFound:Bool!) -> ()  in
                if imageFound == false{
                    self.faceImageView.alpha = 0
                    self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
                    print("NO RESULTS!!!!!")

                }
                if imageFound == true{
                   self.realLoadingLbl.alpha = 0
                    dispatch_async(dispatch_get_main_queue(), {() -> Void in
                        self.faceImageView.image = image

                        self.faceImageView.alpha = 1
                        WikiFaceRec.centerImageViewOnFace(self.faceImageView)
                    })

                }
            })
        } catch WikiFaceRec.WikiFaceError.CouldNotDownloadImage {
            print("Wikipedia not currently open")

        } catch {
            print("error")
            self.faceImageView.alpha = 0
            self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
            print("NO RESULTS")
        }
    }
    return true
}

The code below with the self.realLoadingLbl.text = "No Results Found. Check your spelling and try again." is the part that takes a bit to change. And yes, once again "NO RESULTS!!!" is printed immediately.

if imageFound == false{
    self.faceImageView.alpha = 0
    self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
    print("NO RESULTS!!!!!")
}

Solution

  • You have to handle the imageFound==false case similar to the true case in terms of dispatch_async:

    if !imageFound {
        dispatch_async(dispatch_get_main_queue()) {
            self.faceImageView.alpha = 0
            self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
            print("NO RESULTS!!!!!")
            self.faceImageView.alpha = 0                      
        }
    }