Manish
Manish

Reputation: 1775

Updading UILabel in real time in Swift

I am trying to update UILabel text inside handler of queryPedometerDataFromDate(..) method. Update on UILabel reflects after 20 or 25 seconds. But I see println() output almost in real time. Is there any way to make it quicker? If I update text of UILabel outside this handler, then again it updates in real time.

        pedoman.queryPedometerDataFromDate(yester, toDate: now) {
        (data:CMPedometerData!, errr:NSError!) -> Void in
        //Below statement take 20 to 30 second to reflect on UILabel
        self.StringSteps.text = data.numberOfSteps.stringValue
        //Below statement prints on Output in real time.
        println("Number of Steps \(data.numberOfSteps)")
    }

Upvotes: 1

Views: 1336

Answers (3)

Walid
Walid

Reputation: 720

Swift 4.2

DispatchQueue.main.async {
self.StringSteps.text = data.numberOfSteps.stringValue}

Upvotes: 0

Abubakr Dar
Abubakr Dar

Reputation: 4078

You need to update UILabel on main thread.

dispatch_async(dispatch_get_main_queue(), {
self.StringSteps.text = data.numberOfSteps.stringValue
});

Upvotes: 8

Cihan Tek
Cihan Tek

Reputation: 5409

The block you supplied to the queryPedometerDataFromDate:toDate:withHandler: method gets executed in a serial queue, not on the main thread. You should make all of your UI updates from the main thread. You can do this by using GCD like this:

dispatch_async(dispatch_get_main_queue(), {
   self.StringSteps.text = data.numberOfSteps.stringValue
})

Upvotes: 3

Related Questions