Should my iOS delegation methods always return to the main thread?

This is a “best practice” question that I cannot find to find a good answer for online. I am creating a static code library that provides several delegation methods for feedback on progress, among other things.

The library manages its own queues, so things like downloads are not executed in the main thread, but my question is, should I ensure that my delegate methods are always called in the main thread or is it acceptable to call them from the queue I use? and rely on a developer who uses the library to check it in the main thread if he wants to do user interface updates in my delegate methods?

Cheers, Sam

+8
ios queue delegates grand-central-dispatch
source share
3 answers

You can do it anyway; you just need to document this well.

Some APIs will go to the main thread, some of the threads (or runloop) that you used to get started, and others provide no guarantees whatsoever. Some will even let you go into the GCD queue, which is used for all callbacks.

Remember that delegation / callback can be blocked for a non-trivial amount of time, so if your API needs to resume work as soon as possible, you will of course want to send it to another thread or queue.

Having said all this, if the performance is not critical for you or users of your API, I would go with the most convenient for the developer, which will be the main thread.

+3
source share

When I created my own boot manager, I continued with the delegation method that called the secondary thread, which started the instances of the connection objects, but that was because I had a “controller” that sent the success blocks in the main thread. In my opinion, it depends on what level you are participating in the library, if you think that in most cases the delegate will call a method with routines that include UIKit objects, since they are not thread safe, I will choose them for sending over the main thread . If you think that after delegation the user of your library can continue to develop the data, I would prefer to stay in a different stream, but I will clearly point out the documentation. There is another point: the speed, depending on the three priorities, the secondary stream can be very slow.
[EDIT]
In the download manager, KVO or notification is the best way to handle connections, as Quinn, an Apple engineer in a WWDC video, said.

0
source share

Obviously, you need to call the delegation methods in the main thread, because if I am not mistaken, your delegation methods will be passed to some delegate object (user class).

Suppose you load data into a queue, when you load data, you call the delegate method, passing it to a specific user class object, so it should be in the main thread.

-one
source share

All Articles