0

I found it strange to see the different behaviors for my NSBlockOperation in updating the UI in my app.

For each of the UIButtons (e.g. Thumb-up, Thumb-down, Like, etc), I have them linked to their action method which is similar to the one below using NSBlockOperation.

Take the Like button for example:

@IBAction func likePost(sender: AnyObject) {
        favoriteBtn.enabled = false
        let operation = NSBlockOperation { () -> Void in

            NetworkUtils.sharedInstance.likePost(self.curPost) { (completed, error) -> Void in
                self.favoriteBtn.enabled = true
                self.updateUserLabels(completed) // does not get triggered
            }
        }

        operationQueue.addOperation(operation)
    }

The NetworkUtils makes an API call to Parse that returns a closure for completed (Bool) and error (String?). In this NSBlockOperation, I use the self.updateUserLabels() method to update the UI on mainQueue:

func updateUserLabels(completed: Bool) {

        if completed {
            NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in
                self.likeCount.text = self.curPost.likes; 
            }
        }
    }

The code works well and as expected in the iPhone simulator 5s, however, on the simulator 6s or 6s Plus, it takes much longer to see the Like button to be re-enabled, and the updateUserLabels() method gets called even before the NetworkUtils.sharedInstance.likeQuestion() call returns the closure.

I was wondering why there is such a difference on different iPhone simulators, and how to make them work the same way like on 5s?

TonyW
  • 18,375
  • 42
  • 110
  • 183

1 Answers1

1

it takes much longer to see the Like button to be re-enabled

This is almost certainly because you have neglected to step out to the main thread when performing an operation that involves the interface. Your whole issue would thus presumably be a threading problem.

matt
  • 515,959
  • 87
  • 875
  • 1,141