Stop repeating DispatchQueue.main.async

<p>The&nbsp;<code>MainQueueDecorator</code>&nbsp;acts as an interface between the&nbsp;<code>RemoteItemLoader</code>&nbsp;and the&nbsp;<code>ItemListController</code>, providing a neat way to handle main queue operations.</p> <p>First, we define the&nbsp;<code>ItemService</code>&nbsp;protocol, which allows us to decouple our code from the concrete implementation of the&nbsp;<code>RemoteItemLoader</code>. This also gives us the flexibility to intercept the service and modify its behavior, such as in our case dispatching to the main queue.</p> <p><iframe frameborder="0" height="104" scrolling="no" src="https://medium.com/media/0c75904e3d4a1e2bc73cbc7a6f310473" title="ItemService.swift" width="680"></iframe></p> <p>Next, we make the&nbsp;<code>RemoteItemLoader</code>&nbsp;conform to the&nbsp;<code>ItemService</code>&nbsp;protocol.</p> <p><iframe frameborder="0" height="478" scrolling="no" src="https://medium.com/media/efe82aaf079d17f526b2dd58fb92f114" title="RemoteItemLoader.swift" width="680"></iframe></p> <p>We then replace the concrete class with the&nbsp;<code>ItemService</code>&nbsp;protocol in the&nbsp;<code>ItemListController</code>. This decouples the controller from the implementation details and allows us to work with the abstraction.</p> <p><a href="https://medium.com/@krishnapalsinhgohil/remove-dispatchqueue-main-async-from-everywhere-and-simplify-1fc1c58e8fdf"><strong>Read More</strong></a></p>
Tags: stop repeating