0

Evening I have the following problem:

The Problem

My app is a web based app. It downloads the data from an API and you can display the results through different tableViews.

When the LandingPage is loaded the app starts to download the data. The data are download through many async tasks.

I would like that the user can't use the app until the data are all downloaded, showing an activity indicator.

I was thinking to show the activity indicator as soon the page is loaded and than stop it when the async closure is called.

The problem is that are like 5 async functions, and every async task make others async task.

So is there a way to understand if there is an async task running, and if yes: show the indicator, if not: stop the indicator?

Or, there is a better design patterns to accomplish this?

Community
  • 1
  • 1
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70
  • 1
    Look into using `DispatchGroup`. – rmaddy Jul 19 '17 at 17:45
  • See https://stackoverflow.com/questions/28100847/checking-for-multiple-asynchronous-responses-from-alamofire-and-swift and other similar questions. The use of Alamofire is irrelevant. Use what you need for the actual async tasks. – rmaddy Jul 19 '17 at 17:50

2 Answers2

0

Since you have a lot of async, you should use promise kit:

Promise Kit

With this, you can manage all your requests and even perform different error handling for each one and show or hide loading indicators whenever you want.

Pedro Pinho
  • 652
  • 3
  • 6
0

Solution

as @rmaddy has suggested I used the GroupDispatch

First of all I've create a GroupDispatch object.

For every async call, I've passed the groupDispatch object as parameter, and inside the function i've used group.enter(), so every time the function is called it enters inside the group. While in the closure I've used the group.leave() method.

At the end I've used group.notify to be notified when all the async calls are done.

It works perfectly!

Community
  • 1
  • 1
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70