Search code examples
swiftandroid-activitypropertiesglobalindicator

activity Indicator as global property


I am still very new to programming and sometimes it bites me with very basic concepts. I have an activity indicator defined in my tableviewcontroller as an Outlet.

 @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

The data download to fill the tableview with data is done in a separate file in a class wiht download functions. These functions include the completion handler for the download. Now, if I want to insert the

activityIndicator.stopAnimating()

in the completion part then I get the message "use of unresolved identifier activityIndicator". How can I make the acitivityIndicator a global property, respectively, how can I make the download class/functions recognise the activityIndicator which is defined in the tableViewController? I know this is probably a stupid question for most of you, but I just don't know how to resolve this.


Solution

  • Ideally you don't want the download code to "know" about the activityIndicator. When your viewController calls the download, you could pass another completion handler. Then when the download completion handler runs, call this new completion handler. The viewController knows about the activityIndicator, so it can then stop it. Something (very roughly) along the lines of:

     // In ViewController
         myThing.doTheDownload(completion: {
             dispatch_async(dispatch_get_main_queue(), {
                 self.activityIndicator.stopAnimating()
             })
         })
    
     // In download code
         func doTheDownload(completion completionHandler: (() -> Void)) {
              download(completion: {
                  completionhandler()
              })
         }
    

    Note that activityIndicator is a UI element, and therefore its code must run on the main thread.