1

While implementing a solution based on Realm Mobile Database, RecyclerView and DataBinding I am stuck in receiving a change notification from an edittext view (within recyclerview) that is bound to a realm model. I tried it with RxBinding (my favourite) but without success - when it came to item.setName()... It crashed.

final int row = vh.getAdapterPosition();
    final android.widget.EditText editText = (EditText)vh.itemView.findViewById(R.id.firstLine);
    Subscription editTextSub =
            RxTextView.textChanges(editText)
                    .subscribe(new Action1<CharSequence>() {
                        @Override
                        public void call(CharSequence charSequence) {
                            if (row != RecyclerView.NO_POSITION) {
                                CrngyMaster item = (CrngyMaster) getItem(row);
                                item.setName(charSequence.toString());
                            }
                        }
                    });
    // Make sure to unsubscribe the subscription

I changed the code to:

Subscription editTextSub =
            RxTextView.textChanges(editText)
                    .subscribe(new Action1<CharSequence>() {
                        @Override
                        public void call(CharSequence charSequence) {
                            int row = vh.getAdapterPosition();
                            if (row != RecyclerView.NO_POSITION) {
                                CrngyMaster item = (CrngyMaster) getItem(row);
                                String oldName = item.getName();
                                String modified = charSequence.toString();
                                if(Objects.equals(oldName, modified) == false) {
                                    CrngyMaster original = realm.where(CrngyMaster.class)
                                            .equalTo("mName", oldName)
                                            .findFirst();

                                    realm.beginTransaction();
                                    original.setName(modified);
                                    realm.commitTransaction();
                                }
                            }
                        }
                    });
    // Make sure to unsubscribe the subscription

Now it's working, but: - is this the way it should be implemented? I will add primary keys but I want to prevent the items from being queried when the recyclerview is loaded. Is that possible? And the most important question: how do I unsubscribe?

mischi
  • 25
  • 9
  • It depends on your usecase, because typically the editable copy is a detached object, and then you save it back to the Realm in a single transaction. Also, move the query inside the transaction. – EpicPandaForce Jan 14 '17 at 10:44
  • Generally spoken, even if it's a detached object, I thought it's possible to modify that object. Is that true? I thought I collect the changes (in the detached object) and write that to db only once. – mischi Jan 16 '17 at 11:04

0 Answers0