Reputation: 1331
I have added a spinner to my screen but it does not show. Here is my code pause spinner and play spinner
func pause() {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
}
Here is my restore method
func restore() {
activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
}
I have declared my spinner just inside the class
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
Here is where I am calling the spinner
override func viewDidLoad() {
// myarray = [""]
// var take = NSMutableArray
pause()
var query = PFQuery(className:"Questions")
query.whereKey("Level", equalTo:level)
query.whereKey("Quiz", equalTo:quiz)
query.orderByAscending("Ques")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects.count) scores.")
var i = 0;
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
let pf = object as PFObject
let name = pf["Question"] as String
ql.append(name)
if (flag1 == 0){
myarr.insert(name, atIndex: i)
i++;
}
}
}
println(myarr);
} else {
// Log details of the failure
println("Error: \(error) \(error.userInfo!)")
self.performSegueWithIdentifier("error", sender: self)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(false,forKey:"l"+String(level)+"q"+String(quiz))
defaults.setBool(false,forKey:"l"+String(level)+"p")
}
dispatch_async(dispatch_get_main_queue(), {
self.restore()
})
}
if (flag1==0){
ques = 1
}
questions()
println(ql)
super.viewDidLoad()
// Do any additional setup after loading the view. }
Upvotes: 1
Views: 1890
Reputation: 23078
Your (probably long running) query runs as a background task so your restore()
will be called immediately. You have to change 2 things:
restore()
within the background execution block So, for 2. you have to wrap restore()
in the main thread:
dispatch_async(dispatch_get_main_queue(), {
restore()
})
Upvotes: 1
Reputation: 6806
Your ActivityIndicator starts with your pause
function and stops with your restore
function. In viewDidLoad
, most of the work is carried out by the block you have defined, which runs in the background. The time from pause
to restore
will be tiny. restore
will run long before your block finishes executing.
If you want the spinner to run until the block finshes, you need to do something like
println("Successfully retrieved \(objects.count) scores.")
restore()
inside your block. But, beware, I am not sure what thread your block executes on, and I am not sure if it is correct to call restore
from a background thread. You could try it.
Upvotes: 0