Introduction

Concurrency is a concept that every mobile developer should know. It is the notion of multiple things happening at the same time.

The key is to divide our program into small tasks that can run in parallel, and the final result shouldn’t be affected by the order of the task completion, meaning if task A finishes before task B, the outcome should be the same as if task B finishes before task A.

In the iOS ecosystem, there are several tools to achieve that:

  • NSOperation
  • NSThread
  • Grand Central Dispatcher
  • Reactive Programming frameworks like RxSwift, ReactiveSwift, Combine, etc

When dealing with network requests, the recommended approach is to have an asynchronous task (the thread initiated the task won’t wait until the task is completed to continue executing other tasks) in a background thread (because the main thread should be free for UI updates only). This will guarantee a smooth, freeze-free user experience, that allows the user to continue using the app while the network request eventually finishes some time in the future.

Parallel execution improves the overall speed of the app, if task A takes 2 seconds and task B takes 3 seconds, the 2 tasks running in parallel would take 3 seconds, whereas running in serial (one after the other) would take 5 seconds.


Real-world use case

In the classifieds company I worked, there was a screen on the app to display the search results using a UITableView, and the Business wanted to display Featured (premium/paid) results on top of the regular/free results, a practice widely used in the e-commerce and classified world.

The backend had 2 different endpoints to be consumed, one for regular results, one for premium results, and both require the same parameters. The discussion of whether or not the endpoints should be merged into one is for another post.

#rxswift #ios #networking #ios-app-development #swift #neural networks

Combining Parallel Network Requests with RxSwift
12.50 GEEK