简体   繁体   中英

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.

Thank 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM