0

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.

JohnyTex
  • 3,323
  • 5
  • 29
  • 52
  • 1
    I think it will cause ANR...test it. Tako a look on "What triggers ANR" http://developer.android.com/training/articles/perf-anr.html – Šime Tokić Aug 06 '15 at 07:36
  • I did test it with Thread.sleep(60000) in onCreate() in the ContentProvider and in a separate Activity. I would occassionally get ANR in the Activity but now in the ContentProvider's onCreate(), though I would like to be certain, and also know why and if there are any other downsides if I would perform this "ugly" hack. But I really would have liked is if call() method was on a separate thread -but it isn't right? Thus call() could also cause ANR as far as I can tell? – JohnyTex Aug 06 '15 at 07:40
  • 1
    If sometimes causes ANR then You must avoid it like the documentation says. I think you should take a look at sync adapter, content provider, sqlite db, and how to properly use it together. Take a look at http://www.youtube.com/watch?v=M1ZBjlCRfz0 and then read this http://www.wrox.com/WileyCDA/WroxTitle/Enterprise-Android-Programming-Android-Database-Applications-for-the-Enterprise.productCd-1118183495.html , code examples from book are at https://github.com/wileyenterpriseandroid/Examples – Šime Tokić Aug 06 '15 at 07:41
  • It says: "Android will display the ANR dialog for a particular application when it detects one of the following conditions: No response to an input event (such as key press or screen touch events) within 5 seconds. A BroadcastReceiver hasn't finished executing within 10 seconds." My ContentProvider doesn't have user input, nor a BroadcastReceiver so maybe it would be OK? – JohnyTex Aug 06 '15 at 08:09

4 Answers4

1

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.

Šime Tokić
  • 700
  • 1
  • 9
  • 22
  • This answers says they are running on Binder threads: http://stackoverflow.com/questions/3491747/which-thread-runs-contentprovider And if they were on the UI thread -how could they not delay the UI before returning the Cursor object? – JohnyTex Aug 06 '15 at 07:47
  • OK I think I get it now. query() is called by a thread from another process. This seemed strange to me at first, but I guess it is possible, and it make sense now that I think about it... – JohnyTex Aug 06 '15 at 08:27
0

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.

Sunny Shah
  • 918
  • 1
  • 8
  • 8
0

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.

Ankit Gupta
  • 674
  • 1
  • 6
  • 17
0

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.

diyoda_
  • 5,274
  • 8
  • 57
  • 89