Poster 2 used to using semaphores in two parts when posting contents.
- Posting contents with multiple accounts.
- Posting multiple images when tweeting.
As all network operations are asynchronous, I used semaphores to order the operations one by one. But this architecture had a potential problem. When an error happened, the iterated operations wouldn't stop and the app freeze.
Besides, using semaphores with completions also made the code more complex to understand.
Poster 2 now adapts with Async/Await. It is a framework base on PromiseKit. When using Async/Await, the async code behaves like sync code. So you don't need to deal with completion handlers any more.
- Async code runs as sync code. Easy to understand.
- Using do...try...catch, no completion handlers are needed.
- Async/Await blocks the main thread somehow. So you have to use
asyncblock for the very first calling or view animations won't work.
- Because of the previous reason, you have to deal with the cross-threading if you also using RealmSwift.
- Not all code could change to Async/Await. For example, some frameworks use delegates instead of completion handlers, like AVAudioPlayer.