Permalink
Browse files

Done S10.02- SmarterSyncing, add a flag to make sure the synchronize …

…is not called more than once
  • Loading branch information...
GinaHsu committed Feb 8, 2018
1 parent e82b495 commit d6bf25e50edfe345d5ec442fb7410e4e733cde75
@@ -154,8 +154,8 @@ protected void onCreate(Bundle savedInstanceState) {
getSupportLoaderManager().initLoader(ID_FORECAST_LOADER, null, this);
// TODO (7) Call SunshineSyncUtils's initialize method instead of startImmediateSync
SunshineSyncUtils.startImmediateSync(this);
//SunshineSyncUtils.startImmediateSync(this);
SunshineSyncUtils.initialize(this);
}
/**
@@ -17,18 +17,95 @@
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import com.example.android.sunshine.data.WeatherContract;
public class SunshineSyncUtils {
// TODO (1) Declare a private static boolean field called sInitialized
private static boolean sInitialized;
// TODO (2) Create a synchronized public static void method called initialize
// TODO (3) Only execute this method body if sInitialized is false
// TODO (4) If the method body is executed, set sInitialized to true
// TODO (5) Check to see if our weather ContentProvider is empty
// TODO (6) If it is empty or we have a null Cursor, sync the weather now!
/**
* Creates periodic sync tasks and checks to see if an immediate sync is required.
* If an immediate sync is required, this method will take care of making sure that
* sync occurs.
*
* @param context Context that will be passed to other methods and used to access the
* ContentResolver
*/
synchronized public static void initialize(@NonNull final Context context){
// TODO (3) Only execute this method body if sInitialized is false
/*
* Only perform initialization once per app lifetime. If initialization has already been
* performed, we have nothing to do in this method.
*/
if (sInitialized) return;
// TODO (4) If the method body is executed, set sInitialized to true
sInitialized = true;
// TODO (5) Check to see if our weather ContentProvider is empty
/*
* We need to check to see if our ContentProvider has data to display in our forecast
* list. However, performing a query on the main thread is a bad idea as this may cause
* our UI to lag. Therefore, we create a thread in which we will run the query to check
* the contents of our ContentProvider.
*/
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... voids) {
/* URI for every row of weather data in our weather table*/
Uri forecastQueryUri = WeatherContract.WeatherEntry.CONTENT_URI;
/*
* Since this query is going to be used only as a check to see if we have any data
* (rather than to display data), we just need to PROJECT the ID of each row. In out queries
* where we display data, we need to PROJECT more columns to determine what weather details
* need to be displayed.
* */
String[] projectionColumns = {WeatherContract.WeatherEntry.COLUMN_WEATHER_ID};
String selectionStatement = WeatherContract.WeatherEntry
.getSqlSelectForTodayOnwards();
/* Here, we perform the query to check to see if we have any weather data */
Cursor cursor = context.getContentResolver().query(
forecastQueryUri,
projectionColumns,
selectionStatement,
null,
null);
/*
* A Cursor object can be null for various different reasons. A few are listed below.
* 1) Invalid URI
* 2) a certain ContentProvider's query method returns null
* 3) A remoteException was thrown
*
* Bottom line, it is generally a good idea to check if a Cursor returned
* from a ContentResolver is null.
*
* If the Cursor was null OR if it was empty, we need to sync immediately to
* be able to display data to the user.
*/
// TODO (6) If it is empty or we have a null Cursor, sync the weather now!
if (null == cursor || cursor.getCount() == 0) {
startImmediateSync(context);
}
/* Make sure to close the Cursor to avoid memory leaks! */
cursor.close();
return null;
}
}.execute();
}
/**
* Helper method to perform a sync immediately using an IntentService for asynchronous

0 comments on commit d6bf25e

Please sign in to comment.