best practices for Data Binding Library + Realm?

978 views
Skip to first unread message

Adam Nowak

unread,
Oct 2, 2015, 2:39:40 AM10/2/15
to Realm Java
Do you have any advice, example, best practise with the use of Data Binding Library + Realm togetherWhat to look for?

Nabil Hachicha

unread,
Oct 8, 2015, 5:55:13 AM10/8/15
to Adam Nowak, realm...@googlegroups.com
Hi Adam,

You can wrap your RealmObject as a ViewModel & use the bindable annotation, here is an example.

Model:
public class RealmBook extends RealmObject {
@PrimaryKey private String title; private int price; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }

ModelView extending BaseObservable
public class BookViewModel extends BaseObservable {
    private final RealmBook realmBook;

    public BookViewModel(RealmBook model) {
        this.realmBook = model;
    }

    @Bindable
    public String getTitle() {
        if (realmBook.isValid()) {
            return realmBook.getTitle();
        }
        return "";
    }

    @Bindable
    public int getPrice () {
        return realmBook.getPrice();
    }

    public void setTitle(String title) {
        realmBook.setTitle(title);
        notifyPropertyChanged(BR.title);
    }

    public void setPrice (int price) {
        realmBook.setPrice(price);
        notifyPropertyChanged(BR.price);
    }
}

you then associate your ViewModel with the FormBinding

bookViewModel = new BookViewModel(realm.where(RealmBook.class).findFirst()); 

mBinding.setRealmBook(bookViewModel);

To reflect changes from Realm to the ViewModel you can register a listener

       // listen for changed
        realm.addChangeListener(new RealmChangeListener() {
            @Override
            public void onChange() {
                bookViewModel.notifyChange();
            } 

        });

To save changes to Realm, you can have a button save for example that create a write transaction.
public void onSave (View v) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                // We don't have two way data binding
                bookViewModel.setTitle(mBinding.txtTitle.getText().toString());
                bookViewModel.setPrice(Integer.parseInt(mBinding.txtPrice.getText().toString()));
            }
        });
    }

Cheers,

--
Nabil Hachicha



On Fri, Oct 2, 2015 at 6:39 AM UTC, Adam Nowak <gliwice...@gmail.com> wrote:
Do you have any advice, example, best practise with the use of Data Binding Library + Realm together? What to look for?
--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/1d21eb01-9632-4f56-85bd-fb4bc1eb6ff7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:127170962-1968#}
Reply all
Reply to author
Forward
0 new messages