0

From what I understand, you can only update the UI from the main queue, so why is this code working anyway ?

dispatch_sync(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {

            self.myLabel.text = "Updated text"
        }

The queue provided by QOS_CLASS_BACKGROUND is definitely not the main queue !

Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64

1 Answers1

2

It's not that you can't ever update the UI from a background queue. It's that it isn't defined and it may not work properly so it shouldn't be done.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thank you, I thought there would necessarily be a runtime error when doing that ! – Pop Flamingo May 24 '16 at 20:42
  • 1
    In general no but some UIKit code will show a message and it may cause an exception in a future version of iOS. – rmaddy May 24 '16 at 20:44
  • But than, tiny bit different question, but what is the point of QOS_USER_INTERACTIVE ? Work submitted to this queue won't happen on the main thread right ? So why is this queue still dedicated to user interactive elements ? – Pop Flamingo May 24 '16 at 20:45
  • Because Apple doc says about this QoS "Work that is interacting with the user, such as operating on the main thread, **refreshing the user interface, or performing animations**. If the work doesn’t happen quickly, the user interface may appear frozen. Focuses on responsiveness and performance." – Pop Flamingo May 24 '16 at 20:47
  • 1
    You are asking about the `NSOperation qualityOfService` property I believe. The `NSQualityOfServiceUserInteractive` value doesn't mean the operation will be run on the main queue, it means the operation is critical to interacting with the user so it needs a high priority. It doesn't mean the operation itself is on the main queue. – rmaddy May 24 '16 at 21:49