-2

I'm using Volley and i can get respose from URL.Response jsonObject is like this This is response

I have one problem.I want to pass this data between activities.What is correct way? I'm using realm database.I inserted this json in my database and another activiti,i select this json.Is this correct way? Problem is that,when loading has finish,second activity not starting immediately. This is my java code

 getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            realm.executeTransaction(new Realm.Transaction() {
                                @Override
                                public void execute(Realm realm) {
                                    TrainMainDBModel trainMainDBModel = realm.createObject(TrainMainDBModel.class);
                                    try {
                                        trainMainDBModel.setTrainsJson(jsonObject.getString("data"));
                                        trainMainDBModel.setAdults(Integer.parseInt(adultsValue.getText().toString()));
                                        trainMainDBModel.setChild(Integer.parseInt(childValue.getText().toString()));

                                        realm.insertOrUpdate(trainMainDBModel); 
                                        Intent intent = new Intent(getActivity(), TrainsActivity.class);

                                        startActivity(intent);
                                        getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }


                                }
                            });


                        }

Is it a possible to start second activity immediately,when i have like this large data? thanks

BekaKK
  • 2,173
  • 6
  • 42
  • 80

2 Answers2

0

You should execute the transaction on a background thread.

                   realm.executeTransactionAsync(new Realm.Transaction() {
                            @Override
                            public void execute(Realm realm) {
                                TrainMainDBModel trainMainDBModel = realm.createObject(TrainMainDBModel.class);
                                try {
                                    trainMainDBModel.setTrainsJson(jsonObject.getString("data"));
                                    trainMainDBModel.setAdults(Integer.parseInt(adultsValue.getText().toString()));
                                    trainMainDBModel.setChild(Integer.parseInt(childValue.getText().toString()));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }


                            }
                        }, new Realm.Transaction.OnSuccess() {
                            @Override
                            public void onSuccess() {
                                 Intent intent = new Intent(getActivity(), TrainsActivity.class);
                                 startActivity(intent);    
                                 getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                            }
                        });

Then you can query this object by ID in the other activity.

  TrainMainDBModel trainMainDbModel = realm.where(TrainMainDBModel.class)
                                           .equalTo("id", getIntent().getExtras().getInt("trainId"))
                                           .findFirst();
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
-1

You are performing realm operation on main thread and that will lag your layout so i advice you to save these data in

Volley's on response method + Create Model of your class + Make ArrayList with your model class + then pass it using intent from one activity

it ll be more quick than REALM DB.

Harsh Patel
  • 1,056
  • 2
  • 10
  • 20
  • If you're passing arraylists of data between your intents therefore creating unmanaged copies of your data, then there is no point for you to use Realm as a database – EpicPandaForce May 10 '17 at 13:25
  • Yes but i am giving him advice to avoid the use of Realm. Because this situation can easily be managed by arraylist. – Harsh Patel May 11 '17 at 04:53
  • `Yes but i am giving him advice to avoid the use of Realm.` but he **is** using Realm, in which case you shouldn't avoid it? Also, you shouldn't send large data between activities, because you can hit the transaction too large exception limit. – EpicPandaForce May 11 '17 at 06:10