Asynchronous database call / ActivityIndicator when accessing database
I need to show an Activity Indicator to the user during a database operation that takes some seconds.
I have the UIActivityIndicator configured and working, but when I call [myActivity startAnimating]; and the next call is to do the database operations it never shows me the activity.
I think this could be solved by doing an asynchronously access to the database, but I don't know how to do this.
Than开发者_StackOverflow中文版k for any related information.
The Main UI is probably freezing when you execute your database query, hence the frozen animation.
How about running the database operation in a background thread:
[myActivity startAnimating];
[self performSelectorInBackground:@selector(someMethod) withObject:nil];
-(void)someMethod {
// do something in the background here.
// long running task
[myActivity performSelectorOnMainThread:@selector(stopAnimating) withObject:nil];
}
One thing to note, if you use Core Data on the background thread to update date you must synchronize them so the changes appear on the main thread (read up on Core Data / Threading for more info)
精彩评论