When RuntimeException in doInBackground method we must go to error state.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 16 Dec 2014 00:34:53 +0000 (01:34 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 16 Dec 2014 00:34:53 +0000 (01:34 +0100)
Becareful, there could be multiple AsyncTasks (if user is fast enough). So always
check the trigger AsyncTask conditions.

app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java
app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java

index 18b974f..7cdbeba 100644 (file)
@@ -115,32 +115,31 @@ public class CurrentFragment extends Fragment {
                                if (action.equals(BROADCAST_INTENT_ACTION)) {
                                        final Current currentRemote = (Current) intent.getSerializableExtra("current");
 
-                                       if (currentRemote != null) {
+                                       // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask.
+                                       final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext());
+                               final WeatherLocation weatherLocation = query.queryDataBase();
+                               final PermanentStorage store = new PermanentStorage(context.getApplicationContext());
+                               final Current current = store.getCurrent();
 
-                                               // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask.
-                                               final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext());
-                                   final WeatherLocation weatherLocation = query.queryDataBase();
-                                   final PermanentStorage store = new PermanentStorage(context.getApplicationContext());
-                                   final Current current = store.getCurrent();
+                               if (current == null || !CurrentFragment.this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) {
 
-                                   if (current == null || !CurrentFragment.this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) {
-                                       // 2. Update UI.
-                                       CurrentFragment.this.updateUI(currentRemote);
+                        if (currentRemote != null) {
+                            // 2. Update UI.
+                            CurrentFragment.this.updateUI(currentRemote);
 
-                                       // 3. Update current data.
-                                                       store.saveCurrent(currentRemote);
+                            // 3. Update current data.
+                            store.saveCurrent(currentRemote);
 
                             // 4. Update location data.
                             weatherLocation.setLastCurrentUIUpdate(new Date());
                             query.updateDataBase(weatherLocation);
-                                   }
-
-                                       } else {
-                                               // Empty UI and show error message
-                                               CurrentFragment.this.getActivity().findViewById(R.id.weather_current_data_container).setVisibility(View.GONE);
-                                               CurrentFragment.this.getActivity().findViewById(R.id.weather_current_progressbar).setVisibility(View.GONE);
-                                               CurrentFragment.this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.VISIBLE);
-                                       }
+                        } else {
+                            // Empty UI and show error message
+                            CurrentFragment.this.getActivity().findViewById(R.id.weather_current_data_container).setVisibility(View.GONE);
+                            CurrentFragment.this.getActivity().findViewById(R.id.weather_current_progressbar).setVisibility(View.GONE);
+                            CurrentFragment.this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.VISIBLE);
+                        }
+                               }
                                }
                        }
         };
@@ -412,6 +411,10 @@ public class CurrentFragment extends Fragment {
             } catch (final IOException e) {
                 // logger infrastructure swallows UnknownHostException :/
                 Timber.e(e, "CurrentTask doInBackground exception: " + e.getMessage());
+            } catch (final Throwable e) {
+                // I loathe doing this but we must show some error to our dear user by means
+                // of returning Forecast null value.
+                Timber.e(e, "CurrentTask doInBackground exception: ");
             } finally {
                HTTPClient.close();
             }
index 8424aba..852a0be 100644 (file)
@@ -107,17 +107,17 @@ public class OverviewFragment extends ListFragment {
                                if (action.equals(BROADCAST_INTENT_ACTION)) {
                                        final Forecast forecastRemote = (Forecast) intent.getSerializableExtra("forecast");
 
-                                       if (forecastRemote != null) {
+                                   // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask.
+                                       final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext());
+                               final WeatherLocation weatherLocation = query.queryDataBase();
+                               final PermanentStorage store = new PermanentStorage(context.getApplicationContext());
+                               final Forecast forecast = store.getForecast();
 
-                                               // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask.
-                                               final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext());
-                                   final WeatherLocation weatherLocation = query.queryDataBase();
-                                   final PermanentStorage store = new PermanentStorage(context.getApplicationContext());
-                                   final Forecast forecast = store.getForecast();
+                                   if (forecast == null || !OverviewFragment.this.isDataFresh(weatherLocation.getLastForecastUIUpdate())) {
 
-                                       if (forecast == null || !OverviewFragment.this.isDataFresh(weatherLocation.getLastForecastUIUpdate())) {
-                                               // 2. Update UI.
-                                               OverviewFragment.this.updateUI(forecastRemote);
+                        if (forecastRemote != null) {
+                                           // 2. Update UI.
+                                           OverviewFragment.this.updateUI(forecastRemote);
 
                                                // 3. Update Data.
                                                store.saveForecast(forecastRemote);
@@ -126,13 +126,12 @@ public class OverviewFragment extends ListFragment {
 
                                            // 4. Show list.
                                            OverviewFragment.this.setListShownNoAnimation(true);
-                                       }
-
-                                       } else {
-                                               // Empty list and show error message (see setEmptyText in onCreate)
-                                               OverviewFragment.this.setListAdapter(null);
-                                               OverviewFragment.this.setListShownNoAnimation(true);
-                                       }
+                        } else {
+                            // Empty list and show error message (see setEmptyText in onCreate)
+                            OverviewFragment.this.setListAdapter(null);
+                            OverviewFragment.this.setListShownNoAnimation(true);
+                        }
+                                   }
                                }
                        }
         };
@@ -339,6 +338,10 @@ public class OverviewFragment extends ListFragment {
             } catch (final IOException e) {
                 // logger infrastructure swallows UnknownHostException :/
                 Timber.e(e, "OverviewTask doInBackground exception: " + e.getMessage());
+            } catch (final Throwable e) {
+                // I loathe doing this but we must show some error to our dear user by means
+                // of returning Forecast null value.
+                Timber.e(e, "OverviewTask doInBackground exception: ");
             } finally {
                HTTPClient.close();
             }