Code Monkey home page Code Monkey logo

realm-layered's Introduction

realm-layered

Custom Application

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        RealmHelper.init(this);
    }
}

Helper class for realm

class RealmHelper {

    private static RealmConfiguration cacheRealmConfiguration;
    // just for performance
    private static Realm defaultRealm;
    private static Realm cacheRealm;

    public static void init(Context context) {
        Realm.init(context);
        defaultRealm = Realm.getDefaultInstance();

        cacheRealmConfiguration = new RealmConfiguration.Builder()
                .name("cache.realm")
                .deleteRealmIfMigrationNeeded()
                .build();
        cacheRealm = Realm.getInstance(cacheRealmConfiguration);
    }

    public static Realm getCacheRealm() {
        return Realm.getInstance(cacheRealmConfiguration);
    }
}

MainActivity for get and save data

public class MainActivity extends AppCompatActivity {
...
    private void load() {
        final long start = SystemClock.uptimeMillis();
        dataStore.getUser()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BiConsumer<List<User>, Throwable>() {
                    @Override
                    public void accept(@NonNull List<User> users, @NonNull Throwable throwable) throws Exception {
                        for (User user : users) {
                            // for performance check
                            user.getAge();
                            user.getName();
                        }
                        long end = SystemClock.uptimeMillis();
                        logText.setText("■load:" + (end - start) + "ms realmCount:" + RealmUtil.dumpRealmCount() + " \n" + logText.getText());
                    }
                });
    }
    ...

DataStore class using copyFromRealm

public class DataStore {
...
    public Single<List<User>> getUser() {
        return Single.create(new SingleOnSubscribe<List<User>>() {
            @Override
            public void subscribe(SingleEmitter<List<User>> e) throws Exception {
                Realm realm = RealmHelper.getCacheRealm();
                RealmResults<User> users = realm
                        .where(User.class)
                        .lessThanOrEqualTo("age", 100)
                        .findAll();
                List<User> standAloneList = realm.copyFromRealm(users);
                realm.close();
                e.onSuccess(standAloneList);
            }
        });
    }
}

realm-layered's People

Contributors

takahirom avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.