2

What is the best practice to execute some logic in a blocking way in a concurrent thread (dispatch_sync(MyConcurrentQ) -//DB connection for example) - and poll for the completion of that in the main queue in a blocking way so that I show the next view only when the concurrent thread is done?

Wyetro
  • 8,439
  • 9
  • 46
  • 64
  • Possible duplicate of [What is so bad about singletons?](http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons) – CodeBender Jul 11 '16 at 06:31
  • A singleton isn't an alternative to any of the communication methods you mentioned. A singleton makes it possible for an object, A, to get a reference to some other object, B, but it doesn't provide a method of executing some code in object A in response to a change in object B. Object A would need to poll the property of B, looking for a change, which isn't a good idea. By the way, that "singleton" example you linked to is some of the worst code I have seen. It creates an increasingly deep nest of view controllers and encourages the use of globals (which aren't globals anyway) – Paulw11 Jul 11 '16 at 07:15
  • I'm establishing a connection to some DB using a 3rd party framework, I need to display progress bar until my connection is established successfully, the framework has void APIs and one ConnectionListener from which i set my global variable "Done = True" and poll it in my ViewController to dismiss the progress bar, what is the best practice to achieve this, AM still beginner in swift iOS world – Steve Reynolds Jul 11 '16 at 07:24
  • NSNotification with threads? DON'T. https://stackoverflow.com/questions/15968430/is-nsnotificationcenter-thread-safe – uraimo Jul 11 '16 at 07:41
  • Check out `NSOperation` & `NSOperationQueue`. They are high level wrapper around GCD which allow you to add dependencies and configure the maximum level of concurrency. if you need more help, check this [WWDC 2015 video](https://developer.apple.com/videos/play/wwdc2015/226/) – Code Different Jul 11 '16 at 12:27

1 Answers1

-2

i found the answer at apple documentation Apple's docs on the subject say: "Regular notification centers deliver notifications on the thread in which the notification was posted. [...] At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. [...] In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread." https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Threading.html#//apple_ref/doc/uid/20001289-CEGJFDFG