Posting a question for the first time here. Just started programming and coding my first app.
I have an Application class file that has the method fetchUpdates():
public synchronized void fetchUpdates() {
String[] mURLs = getURLs();
new DownloadJSONData().execute(mURLs[0], mURLs[1]);
}
DownloadJSONData is an asyncTask
that gets updates from the server and the onPostExecute
method updates an sqlite database using a JSON array from the server.
Different components (widgets and activities) of the app call this method to get updates from the server to update the database.
The question: Does synchronized method run on a separate thread from the UI thread? If so, there should be no problem in moving the code in the DownloadJSONData
to the synchronized fetchUpdates()
method right? And if there is a holdup in connecting to the server or downloading the data from the server, it shouldn't block the UI thread right?
The motive: I'm trying to change the fetchUpdates()
method to return a boolean indicating whether the database was updated or not. However, as it is now (I think) fetchUpdates()
method finishes before the asyncTask
's onPostExecute
method and, hence, fetchUpdates()
method cannot indicate whether the call to DownloadJSONData()
updated the database. I need the app components that call the fetchUpdates()
method to behave differently if the database was updated after the call.
P.S. Since I'm new to programming simple and detailed explanations would be really helpful.
Thanks!