Search in sources :

Example 1 with Person

use of io.realm.examples.rxjava.model.Person in project realm-java by realm.

the class GotchasActivity method testSubscribeOn.

/**
     * Shows how to be careful with `subscribeOn()`
     */
private Subscription testSubscribeOn() {
    Subscription subscribeOn = realm.asObservable().map(new Func1<Realm, Person>() {

        @Override
        public Person call(Realm realm) {
            return realm.where(Person.class).findAllSorted("name").get(0);
        }
    }).subscribeOn(//
    Schedulers.io()).subscribe(new Action1<Person>() {

        @Override
        public void call(Person person) {
        // Do nothing
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            showStatus("subscribeOn: " + throwable.toString());
        }
    });
    // Use Realms Async API instead
    Subscription asyncSubscribeOn = realm.where(Person.class).findAllSortedAsync("name").get(0).<Person>asObservable().subscribe(new Action1<Person>() {

        @Override
        public void call(Person person) {
            showStatus("subscribeOn/async: " + person.getName() + ":" + person.getAge());
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            showStatus("subscribeOn/async: " + throwable.toString());
        }
    });
    return new CompositeSubscription(subscribeOn, asyncSubscribeOn);
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Realm(io.realm.Realm) Person(io.realm.examples.rxjava.model.Person)

Example 2 with Person

use of io.realm.examples.rxjava.model.Person in project realm-java by realm.

the class RetrofitExample method onResume.

@Override
protected void onResume() {
    super.onResume();
    // Load all persons and merge them with their latest stats from GitHub (if they have any)
    subscription = realm.where(Person.class).isNotNull("githubUserName").findAllSortedAsync("name").asObservable().filter(new Func1<RealmResults<Person>, Boolean>() {

        @Override
        public Boolean call(RealmResults<Person> persons) {
            // We only want the list once it is loaded.
            return persons.isLoaded();
        }
    }).flatMap(new Func1<RealmResults<Person>, Observable<Person>>() {

        @Override
        public Observable<Person> call(RealmResults<Person> persons) {
            // Emit each person individually
            return Observable.from(persons);
        }
    }).flatMap(new Func1<Person, Observable<GitHubUser>>() {

        @Override
        public Observable<GitHubUser> call(Person person) {
            // get GitHub statistics. Retrofit automatically does this on a separate thread.
            return api.user(person.getGithubUserName());
        }
    }).map(new Func1<GitHubUser, UserViewModel>() {

        @Override
        public UserViewModel call(GitHubUser gitHubUser) {
            // Map Network model to our View model
            return new UserViewModel(gitHubUser.name, gitHubUser.public_repos, gitHubUser.public_gists);
        }
    }).observeOn(// Retrofit put us on a worker thread. Move back to UI
    AndroidSchedulers.mainThread()).subscribe(new Action1<UserViewModel>() {

        @Override
        public void call(UserViewModel user) {
            // Print user info.
            TextView userView = new TextView(RetrofitExample.this);
            userView.setText(String.format(Locale.US, "%s : %d/%d", user.getUsername(), user.getPublicRepos(), user.getPublicGists()));
            container.addView(userView);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
}
Also used : Action1(rx.functions.Action1) Observable(rx.Observable) TextView(android.widget.TextView) Func1(rx.functions.Func1) Person(io.realm.examples.rxjava.model.Person) RealmResults(io.realm.RealmResults)

Example 3 with Person

use of io.realm.examples.rxjava.model.Person in project realm-java by realm.

the class MyApplication method createTestData.

// Create test data
private void createTestData() {
    final Random r = new Random(42);
    Realm realm = Realm.getDefaultInstance();
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            for (Map.Entry<String, String> entry : testPersons.entrySet()) {
                Person p = realm.createObject(Person.class);
                p.setName(entry.getKey());
                p.setGithubUserName(entry.getValue());
                p.setAge(r.nextInt(100));
            }
        }
    });
    realm.close();
}
Also used : Random(java.util.Random) Realm(io.realm.Realm) Person(io.realm.examples.rxjava.model.Person)

Example 4 with Person

use of io.realm.examples.rxjava.model.Person in project realm-java by realm.

the class AnimationActivity method onResume.

@Override
protected void onResume() {
    super.onResume();
    // Load all persons and start inserting them with 1 sec. intervals.
    // All RealmObject access has to be done on the same thread `findAllAsync` was called on.
    // Warning: This example doesn't handle back pressure well.
    subscription = realm.where(Person.class).findAllAsync().asObservable().flatMap(new Func1<RealmResults<Person>, Observable<Person>>() {

        @Override
        public Observable<Person> call(RealmResults<Person> persons) {
            return Observable.from(persons);
        }
    }).zipWith(Observable.interval(1, TimeUnit.SECONDS), new Func2<Person, Long, Person>() {

        @Override
        public Person call(Person person, Long tick) {
            return person;
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Person>() {

        @Override
        public void call(Person person) {
            TextView personView = new TextView(AnimationActivity.this);
            personView.setText(person.getName());
            container.addView(personView);
        }
    });
}
Also used : Action1(rx.functions.Action1) TextView(android.widget.TextView) Person(io.realm.examples.rxjava.model.Person) Observable(rx.Observable) RealmResults(io.realm.RealmResults) Func2(rx.functions.Func2)

Example 5 with Person

use of io.realm.examples.rxjava.model.Person in project realm-java by realm.

the class GotchasActivity method testDistinct.

/**
     * Shows how to to be careful when using `distinct()`
     */
private Subscription testDistinct() {
    Observable<Person> personObserver = realm.asObservable().map(new Func1<Realm, Person>() {

        @Override
        public Person call(Realm realm) {
            return realm.where(Person.class).findAllSorted("name").get(0);
        }
    });
    // distinct() and distinctUntilChanged() uses standard equals with older objects stored in a HashMap.
    // Realm objects auto-update which means the objects stored will also auto-update.
    // This makes comparing against older objects impossible (even if the new object has changed) because the
    // cached object will also have changed.
    // Use a keySelector function to work around this.
    Subscription distinctItemTest = personObserver.distinct().subscribe(new Action1<Person>() {

        @Override
        public void call(Person p) {
            showStatus("distinct(): " + p.getName() + ":" + p.getAge());
        }
    });
    Subscription distinctKeySelectorItemTest = personObserver.distinct(new // Use a keySelector function instead
    Func1<Person, Integer>() {

        @Override
        public Integer call(Person p) {
            return p.getAge();
        }
    }).subscribe(new Action1<Person>() {

        @Override
        public void call(Person p) {
            showStatus("distinct(keySelector): " + p.getName() + ":" + p.getAge());
        }
    });
    return new CompositeSubscription(distinctItemTest, distinctKeySelectorItemTest);
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Func1(rx.functions.Func1) Realm(io.realm.Realm) Person(io.realm.examples.rxjava.model.Person)

Aggregations

Person (io.realm.examples.rxjava.model.Person)6 TextView (android.widget.TextView)3 Realm (io.realm.Realm)3 RealmResults (io.realm.RealmResults)3 Action1 (rx.functions.Action1)3 Func1 (rx.functions.Func1)3 Observable (rx.Observable)2 Subscription (rx.Subscription)2 CompositeSubscription (rx.subscriptions.CompositeSubscription)2 RxTextView (com.jakewharton.rxbinding.widget.RxTextView)1 TextViewTextChangeEvent (com.jakewharton.rxbinding.widget.TextViewTextChangeEvent)1 Random (java.util.Random)1 Func2 (rx.functions.Func2)1