Search code examples
iosswiftasynchronousuiactivityindicatorview

Swift: Activity Indicator with Async Web tasks


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?


Solution

  • 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!