Testing code that uses DispatchQueue.main.async | iOS Lead Essentials Community Q&A

preview_player
Показать описание
In this episode, Caio replies to a question we received from Tulio in the private iOS Lead Essentials Slack community:

"How can I test code that dispatches work to the main DispatchQueue asynchronously? If I remove the thread handling code, my test succeeds."

Article ↴

Q&A playlist ↴

---

Like and share this content with someone that you believe will benefit from it. See you in the next episode.

Subscribe to our channel ↴

You can also find us on ↴

Caio ↴

Mike ↴

Join the iOS Lead Essentials program ↴

Visit our website ↴
Рекомендации по теме
Комментарии
Автор

Super interesting topic. Thanks for the video!

FabioFerreroPlus
Автор

Hello Caio. Good content!
This is really good insight, although I'm trying to understand something.

At the beginning of the video, you mentioned a common alternative was to move the dispatch call into the service implementation which doesn't seem like the best solution, to which I can agree. But using this decorator pattern feels like is achieving the same thing but now it terms of individual Service instances (ie, the decorated service is now bound to the main queue), what if I still want to run the result through other layers before the view for processing (think of going through an Interactor > Presenter > finally View )? It doesn't feel right to have any corresponding layer dispatch to a background queue again for processing. Or does it make sense to apply the decorator then to the last layer right before the View (ie decorate the presenter methods that call the view)?

I understand this video is a very simple use case, but it just ocurred to me on more complex projects

In any case, thanks for the content! You guys do a good work. I've been learning a lot from you recently.

isaacdelgado
Автор

Do you have any same solution for scheduler in combine?

TungNguyen-qrrn
Автор

Doesn’t the main thread run more than one queue? Or is it a special case? I’ve always struggled to check if I am in the main QUEUE to update the UI…

alejandroivan
Автор

Please correct me if am wrong here.
Can we use that decorator at the place of using expectations and waitForExpectaion to test asyc peice of code ?

ArpanDixit
Автор

Thanks, very nice explanations as always!
But does this mean that we have to write an associated Decorator for each service we have in the app?
I find it more practical to replace the DispatchQueue.main object with some test stub using dependency injection rather than having to maintain a Decorator for each service interface.
Wouldn't it be possible in some way to design a generic Decorator that can wrap any Service protocol?

one-week-apps
Автор

Using first approach, your view controller become dependent on MainQueueDispatchDecorator, not on “Service” interface, because there is no more dispatching to the main thread in VC.
Is this correct dependency strategy?

danielv
Автор

How would we do this with an async await based service?

So instead of

service.load { [weak self] text in 
DispatchQueue.main.async {
self?.label.text = text
}
}

We have...

let text = await service.load()
DispatchQueue.main.async {
self.label.text = text
}

?

kaylanx
Автор

Super interesting topic. Thanks for the video!

learnwith