Commit 5a98fc28 authored by Varun Patil's avatar Varun Patil

Disallow DB queries on main thread

parent e0b48bee
...@@ -18,7 +18,7 @@ public abstract class AppDatabase extends RoomDatabase { ...@@ -18,7 +18,7 @@ public abstract class AppDatabase extends RoomDatabase {
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "database") Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "database")
// allow queries on the main thread. // allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example. // Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries() // .allowMainThreadQueries()
.build(); .build();
} }
return INSTANCE; return INSTANCE;
......
...@@ -93,12 +93,7 @@ public class BodyFragment extends Fragment { ...@@ -93,12 +93,7 @@ public class BodyFragment extends Fragment {
/* Initialize */ /* Initialize */
appDatabase = AppDatabase.getAppDatabase(getContext()); appDatabase = AppDatabase.getAppDatabase(getContext());
Body[] inLocalDb = appDatabase.dbDao().getBody(min_body.getBodyID()); new getDbBody().execute(min_body.getBodyID());
if (inLocalDb.length > 0) {
displayBody(inLocalDb[0]);
} else {
updateBody();
}
bodySwipeRefreshLayout=getActivity().findViewById(R.id.body_swipe_refresh_layout); bodySwipeRefreshLayout=getActivity().findViewById(R.id.body_swipe_refresh_layout);
bodySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { bodySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override @Override
...@@ -116,7 +111,7 @@ public class BodyFragment extends Fragment { ...@@ -116,7 +111,7 @@ public class BodyFragment extends Fragment {
if (response.isSuccessful()) { if (response.isSuccessful()) {
Body body = response.body(); Body body = response.body();
appDatabase.dbDao().insertBody(body); new insertDbBody().execute(body);
displayBody(body); displayBody(body);
bodySwipeRefreshLayout.setRefreshing(false); bodySwipeRefreshLayout.setRefreshing(false);
...@@ -226,6 +221,30 @@ public class BodyFragment extends Fragment { ...@@ -226,6 +221,30 @@ public class BodyFragment extends Fragment {
} }
} }
private class insertDbBody extends AsyncTask<Body, Void, Integer> {
@Override
protected Integer doInBackground(Body... body) {
appDatabase.dbDao().insertBody(body[0]);
return 1;
}
}
private class getDbBody extends AsyncTask<String, Void, Body[]> {
@Override
protected Body[] doInBackground(String... id) {
return appDatabase.dbDao().getBody(min_body.getBodyID());
}
@Override
protected void onPostExecute(Body[] result) {
if (result.length > 0) {
displayBody(result[0]);
} else {
updateBody();
}
}
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment