Onichan
Onichan

Reputation: 4516

Hide Activity Indicator Does Not Work

The activity indicator starts, but does not stop when the hide function is called. I've tried putting the hide function in various places, and it still does not hide.

Hide activity indicator: Q0ViewController().hideActivityIndicator(self.view)

I'm using the swift utility function found here: https://github.com/erangaeb/dev-notes/blob/master/swift/ViewControllerUtils.swift

Start activity indicator

override func viewDidLoad() {
    super.viewDidLoad()
    Q0ViewController().showActivityIndicator(self.view)
    self.locationManager.delegate = self //location manager start
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
}

Hide activity indicator after query:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
        if (error != nil) {
            println("Error:" + error.localizedDescription)
            //return
        }
        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.displayLocationInfo(pm)
            currentLoc = manager.location
            currentLocGeoPoint = PFGeoPoint(location:currentLoc)
            var query = PFQuery(className:"test10000")
            query.whereKey("RestaurantLoc", nearGeoPoint:currentLocGeoPoint, withinMiles:100) //filter by miles
            query.limit = 1000 //limit number of results
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]!, error: NSError!) -> Void in
                if objects != nil {
                    unfilteredRestaurantArray = objects
                    originalUnfilteredArray = objects
                    println(objects)
                } else {
                    println("error: \(error)")
                }
            Q0ViewController().hideActivityIndicator(self.view) //HIDE
            }
        } else {
            println("error: \(error)")
        }
    })
}

It is not an issue with the main queue as dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), { ()->() in does not resolve the issue.

Upvotes: 0

Views: 580

Answers (2)

Onichan
Onichan

Reputation: 4516

Similar to what Joshua suggested, just replaced:

Q0ViewController().showActivityIndicator(self.view)
and
Q0ViewController().hideActivityIndicator(self.view)

To:

self.showActivityIndicator(self.view)
and
self.hideActivityIndicator(self.view)

Upvotes: 0

Joshua Finch
Joshua Finch

Reputation: 1595

Looks like you're creating a new instance of the "Q0ViewController" each time.

Instead I would suggest retaining the initial instance as a property on your class:

// As a variable on the class instance
let myViewController = Q0ViewController()

// Initially show the activity indicator
self.myViewController.showActivityIndicator(self.view)

// Hide the activity indicator
self.myViewController.hideActivityIndicator(self.view)

Hopefully this helps!

Upvotes: 1

Related Questions