Store all the information at once using Realm in Background thread Android

426 views Asked by At

I am developing an android app, where I am using Relam as local Database. I have rest api one for all the user information and other is for news information. I have two buttons. one for showing the user information and other is for showing the news information. Now, with my code what is done, after login, I need to click the button at first for user information and news information accordingly and then I can see those images and news. but if I connection off, just after login, the data is not showing in the view. I am explaining my code in detail.Also I am having problem in image loading. How can I Make the funtionality so that user can get all the information just after login.I am reallly sorry for such a long code. It would be really helpful for me if someone tell me how can I modify my code to store all data in background thread, so that user get all information just after login.

The part of the login page where I want to start

private void loginUser(final String mEmail, final String mPassword) {

    final GlobalClass globalClass = new GlobalClass();
    globalClass.setEmail_info( mEmail );

    setFilePath();

    RequestQueue queue = Volley.newRequestQueue( LoginPage.this );

    StringRequest strReq = new StringRequest( Request.Method.POST,
            LOGIN_URL, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d( TAG, "Register Response: " + response.toString() );

            //parse your response here
            if (response.contains( "overview" )) {
                showProgress( true );

                globalClass.setImage_urlpath( Constants.HTTP.PHOTO_URL + mEmail);
                String str = globalClass.readDatafromStorage();
                Log.d("----After Login---",str);

               if ( !str.contains("ACTIVATE") ) {
                   Log.d( "----After Login---", "After Login" );

                }

                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString(KEY_EMAIL, mEmail);
                editor.putString(KEY_PASSWORD, mPassword);
                editor.commit();

                showProgress(false);
                Intent loginIntent = new Intent(LoginPage.this, MainOptionPage.class);
                loginIntent.putExtra(KEY_EMAIL, mEmail);
                startActivity(loginIntent);

            } else {
                userEmail.setError(getString(R.string.error_incorrect_login));
                userEmail.requestFocus();

            }
        }

    }, new Response.ErrorListener() {
        @Override
       ....

Here is my code for User Page

    public class MyColleaguesPage extends AppCompatActivity implements ColleagueController.UserCallbackListener {

     private List<MyColleagueModel> myColleagueList = new ArrayList<>();

    private Realm colleagueRealm;
    private RealmResults<MyColleagueModel> colleagueResult;

    private List<MyColleagueModel> filteredModelList;
    private RealmChangeListener realmListener;
    private static final String DIALOG_TAG = "EmployeeDialog";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mycolleagues_layout);



        configViews();

    }

    private void configViews() {
        recyclerView = this.findViewById(R.id.colleagues_recycler);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(MyColleaguesPage.this));
        recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());

        colleagueRealm = Realm.getDefaultInstance();

        RealmResults<MyColleagueModel> results = colleagueRealm.where(MyColleagueModel.class).findAll();

        for (int i = 0; i < results.size(); i++) {

            myColleagueList.add(results.get(i));
        }
        adapter = new MyColleaguesAdapter(myColleagueList,getApplicationContext());
        //adapter = new MyColleaguesAdapter(myColleagueList,getApplicationContext());
        Log.d( "adapter value is"+"", String.valueOf( adapter ) );
        recyclerView.setAdapter(adapter);
    }


    //successful
    @Override
    public void onFetchStart() {

    }

    @Override
    public void onFetchProgress(ColleagueModel colleague) {
        //adapter.addColleague(colleague);
    }

    @Override
    public void onFetchProgress(List<ColleagueModel> colleagueList) {

    }

    @Override
    public void onFetchComplete() {

    }

    @Override
    public void onFetchFailed() {

    }
   }

Here is my controller class for my colleague page

    public class ColleagueController {

    private static final String TAG = ColleagueController.class.getSimpleName();
    private UserCallbackListener mListener;
    private ColleagueResApiManager mApiManager;


    Realm myColleague_realm;    
    public ColleagueController() {
        mApiManager = new ColleagueResApiManager();
    }


public void startFetching(){
    myColleague_realm = Realm.getDefaultInstance();

    mApiManager.getColleagueApi().getColleague(new Callback<String>() {
        @Override
        public void success(String s, Response response) {
            Log.d(TAG, "JSON :: " + s);
            try {
                JSONArray array = new JSONArray(s);

                for(int i = 0; i < array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    Log.d("-----Start Fetching---", object.optString( "name" ));

                        myColleague_realm.beginTransaction();
                        MyColleagueModel mycolleague = myColleague_realm.createObject( MyColleagueModel.class );

                       mycolleague.setName( object.optString( "name" ) );
                       .... data  ) );



                    myColleague_realm.commitTransaction();

                }

            } catch (JSONException e) {
                mListener.onFetchFailed();
            }

           // mListener.onFetchComplete();
        }
        @Override
        public void failure(RetrofitError error) {
            Log.d(TAG, "Error :: " + error.getMessage());
            if (mListener != null) {
                mListener.onFetchComplete();
            }
        }
    });

}




    public interface UserCallbackListener{

        void onFetchComplete();
        void onFetchFailed();
    }
}

In the same way I have other page news option page where I am shoing the news data. Here is my news page.

    public class NewsPage extends AppCompatActivity{

    private RecyclerView recyclerView;
    private NewsAdapter adapter;
    private Realm newsRealm;


    private List<NewsRealmModel> mNewsList;
    private List<NewsRealmModel> filteredModelList;

    private NewsController mController;

    Constant constant;
    SharedPreferences app_preferences;
    int appTheme;
    int themeColor;
    int appColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.news_page_layout);



        configViews();

    }

    private void configViews() {
        recyclerView = this.findViewById(R.id.news_recycler);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(NewsPage.this));
        recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());

        Realm newsRealm = Realm.getDefaultInstance();
        RealmResults<NewsRealmModel> temp = newsRealm.where(NewsRealmModel.class).findAll();
        mNewsList = new ArrayList<>();
        for (int i = 0; i < temp.size(); i++) {

            mNewsList.add(temp.get(i));
        }

        adapter = new NewsAdapter(mNewsList,getApplicationContext());
        Log.d( "adapter value is"+"", String.valueOf( adapter ) );
        recyclerView.setAdapter(adapter);
     }


   } 

And the new Controller Class

public class NewsController {
private static final String TAG = NewsController.class.getSimpleName();
private UserCallbackListener mListener;
private NewsRestApiManager mApiManager;
private AppImage appImages;

Realm myNews_realm;
ArrayList<String> title_list = new ArrayList<>();
GlobalClass globalClass = new GlobalClass();

public NewsController(UserCallbackListener listener) {
    mListener = listener;
    mApiManager = new NewsRestApiManager();
}


public void startFetching() {
    myNews_realm = Realm.getDefaultInstance();
    mApiManager.getNewsApi().getNews(new Callback<String>() {

        @Override
        public void success(String s, Response response) {
            Log.d(TAG, "JSON :: " + s);

            try {
                JSONArray array = new JSONArray(s);

                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    Log.d("-----Start Fetching---", jsonObject.optString("title"));

                    if (!myNews_realm.isInTransaction()) {

                        myNews_realm.beginTransaction();

                        NewsRealmModel news = new NewsRealmModel();
                       ....... data 
                        }

                        myNews_realm.copyToRealm(news);
                        myNews_realm.commitTransaction();

                        mListener.onFetchProgressNews(news);

                    } else {
                        myNews_realm.commitTransaction();
                    }


                }
            } catch (JSONException e) {
                mListener.onFetchFailed();
            }

            mListener.onFetchComplete();
        }

        @Override
        public void failure(RetrofitError error) {
            Log.d(TAG, "Error :: " + error.getMessage());
            mListener.onFetchComplete();
        }
    });

}
public interface UserCallbackListener {


    void onFetchProgressNews(NewsRealmModel news);



    void onFetchComplete();

    void onFetchFailed();
}

}

1

There are 1 answers

1
EpicPandaForce On

To do your database processing on a background thread using Volley, you need to extend Request<T> and do the Realm write in parseNetworkResponse method.

public class RealmGsonObjectRequest<T, M extends RealmModel> extends Response<Void> {
  ...
  @Override
  protected Response<Void> parseNetworkResponse(
                            NetworkResponse response) {
    try {
      String json = new String(response.data,
          HttpHeaderParser.parseCharset(response.headers));
      T data = gson.fromJson(json, clazz);

      // write the downloaded data into the Realm on bg thread
      try(Realm r = Realm.getDefaultInstance()) {
        M model = mapper.toModel(data);
        r.executeTransaction((realm) -> {
          realm.insertOrUpdate(model);
        });
      }

      return Response.success(null,
         HttpHeaderParser.parseCacheHeaders(response)
      );
    } // handle errors
}

You might need a RealmGsonListRequest as well.

public class RealmGsonListRequest<T, M extends RealmModel> extends Response<Void> {
  ...
  @Override
  protected Response<Void> parseNetworkResponse(
                            NetworkResponse response) {
    try {
      String json = new String(response.data,
          HttpHeaderParser.parseCharset(response.headers));
      List<T> data = gson.fromJson(json, new TypeToken<ArrayList<T>>() {}.getType());

      // write the downloaded data into the Realm on bg thread
      try(Realm r = Realm.getDefaultInstance()) {
        M model = mapper.toModel(data);
        r.executeTransaction((realm) -> {
          realm.insertOrUpdate(model);
        });
      }

      return Response.success(null,
         HttpHeaderParser.parseCacheHeaders(response)
      );
    } // handle errors
}

For more information, refer to the official Volley tutorial on how to create a custom Volley request type.