JohnyTex
JohnyTex

Reputation: 3481

Can I do lengthy operations i ContentProvider's onCreate()?

For certain reasons I would like to initialize my data-fetching in onCreate() because I would like to use call() instead of query().

call() method does seem to run on the main thread and so does onCreate(). What I would like to know is if I can do lengthy operations in onCreate() without risking "Application not responding"-dialog or other unwanted or bad behaviour?

Why is call() not taking place on separate threads, as is the case for query()? Can call() cause "Application not responding"-dialog?

Note 1: Application startup delay is acceptable if it doesn't cause "Application not responding"-dialog or such.
Note 2: I am doing a special ContentProvider that is fetching things from the Internet.

Upvotes: 0

Views: 617

Answers (4)

diyoda_
diyoda_

Reputation: 5420

OnCreate() method runs on the UI Thread, So what ever android prohibits of doing on UI thread, applies here. Eg: I/O, Networking heavy code should not be kept in this method.

Upvotes: 0

Šime Tokić
Šime Tokić

Reputation: 710

No, You should not do lengthy operations. "...It must not perform lengthy operations, or application startup will be delayed."

SOURCE: http://developer.android.com/reference/android/content/ContentProvider.html#onCreate()

As for the last question; query(...) method does not run on seperate thread, neither the call(...) method. If you want to run on seperate thread, You should create the thread manually (for example via AsyncTask) or use Loaders.

Upvotes: 1

Ankit Gupta
Ankit Gupta

Reputation: 672

Doing Lengthy operation in onCreate() method is not a good idea at all. As it will run on Main UI thread and you will get ANR message. Better idea is to use AsyncTaskLoaders to fetch data. And if you are fetching the data from local SQLite database, use cursorLoaders. Using AsynctaskLoader will make the fetching operation run on separate thread. And your UI will run smoothly. And if there is underlying data change u can use content observer to see the changes and it will affect UI data automatically.

Upvotes: 0

Sunny Shah
Sunny Shah

Reputation: 928

No you should not do lengthy operation in onCreate() of Content Provider.

From the Official Docs.

This method is called for all registered content providers on the application main thread at application launch time. It must not perform lengthy operations, or application startup will be delayed.

Upvotes: 0

Related Questions