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 {
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "database")
// allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
// .allowMainThreadQueries()
.build();
}
return INSTANCE;
......
......@@ -93,12 +93,7 @@ public class BodyFragment extends Fragment {
/* Initialize */
appDatabase = AppDatabase.getAppDatabase(getContext());
Body[] inLocalDb = appDatabase.dbDao().getBody(min_body.getBodyID());
if (inLocalDb.length > 0) {
displayBody(inLocalDb[0]);
} else {
updateBody();
}
new getDbBody().execute(min_body.getBodyID());
bodySwipeRefreshLayout=getActivity().findViewById(R.id.body_swipe_refresh_layout);
bodySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
......@@ -116,7 +111,7 @@ public class BodyFragment extends Fragment {
if (response.isSuccessful()) {
Body body = response.body();
appDatabase.dbDao().insertBody(body);
new insertDbBody().execute(body);
displayBody(body);
bodySwipeRefreshLayout.setRefreshing(false);
......@@ -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
public View onCreateView(LayoutInflater inflater, ViewGroup container,
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