{"id":18643617,"url":"https://github.com/lemberg/android-realm-sample","last_synced_at":"2025-04-11T12:30:46.369Z","repository":{"id":70144928,"uuid":"47748111","full_name":"lemberg/android-realm-sample","owner":"lemberg","description":null,"archived":false,"fork":false,"pushed_at":"2016-01-23T18:13:40.000Z","size":131,"stargazers_count":25,"open_issues_count":0,"forks_count":6,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-25T13:39:21.627Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lemberg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-10T08:25:51.000Z","updated_at":"2019-08-30T23:55:00.000Z","dependencies_parsed_at":"2023-04-19T08:18:09.462Z","dependency_job_id":null,"html_url":"https://github.com/lemberg/android-realm-sample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemberg%2Fandroid-realm-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemberg%2Fandroid-realm-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemberg%2Fandroid-realm-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemberg%2Fandroid-realm-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lemberg","download_url":"https://codeload.github.com/lemberg/android-realm-sample/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248401927,"owners_count":21097328,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T06:07:35.434Z","updated_at":"2025-04-11T12:30:46.033Z","avatar_url":"https://github.com/lemberg.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n**Realm database**\n\nRealm Java enables you to efficiently write your app’s model layer in a safe, persisted and fast way. \n\n**Realm is a replacement for SQLite \u0026 Core Data**\n\n - *Easy to Use*\n\n\tRealm is not an ORM on top of SQLite. Instead it uses its own persistence engine, built for simplicity (\u0026 speed).\n\n - *Fast*\n\n Thanks to its zero-copy design, Realm is much faster than an ORM, and is often faster than raw SQLite as well. \n\n - *Cross-Platform*\n\n You can share Realm files across platforms and use the same high-level models.\n\nSee graphic performance comparison to other databases [here](https://realm.io/news/realm-for-android/#realm-for-android).\n\n**Prerequisites**\n\n - [Realm](https://realm.io/) do not support Java outside of Android at the moment.\n - [Android Studio](http://developer.android.com/sdk/index.html?gclid=COOv_bPoo8oCFcoGcwodcJ4AVQ) \u003e= 0.8.6\n - A recent version of the Android SDK.\n - JDK version \u003e=7.\n - Support all [Android versions](http://developer.android.com/about/dashboards/index.html) since API Level 9 (Android 2.3 Gingerbread \u0026 above).\n\n**Here how it looks like**\n\n```java\npublic class User extends RealmObject {\n\t@PrimaryKey\n    private long id;\n    private String firstName;\n\t//basic implementation\n}\n```\n```java\n//write single User object\n realm.executeTransaction(new Realm.Transaction() {\n            @Override\n            public void execute(Realm bgRealm) {\n                bgRealm.copyToRealmOrUpdate(user);\n            }\n        });\n```\n```java\n//Query to look for all users\nrealm.where(User.class).findAll();\n```\n\n**Installation**\n\n 1. Make sure your project uses jcenter as a dependency repository\n        (default on latest version of the Android Gradle plugin).\n\n 2. Add compile `'io.realm:realm-android:0.87.2'` to the dependencies of\n        your project. See latest version on [realm.io](https://realm.io/docs/java/latest/)\n\n 3. In the Android Studio menu: Tools-\u003eAndroid-\u003eSync Project with Gradle\n        Files.\n\n**Best Practices**\n\n*UI or not UI?*\n\n - Typically Realm is fast enough to read and write data on Android’s\n   main thread. However, write transactions are blocking across threads\n   so in order to prevent accidental Application Not Responding Errors (ANR’s) there are advise to perform\n   all Realm write operations on a background thread (not Android’s main\n   thread).\n\n```java\nRealmResults\u003cUser\u003e userList = realm.where(User.class).findAllAsync();\nuserList.addChangeListener(new RealmChangeListener() {\n            @Override\n            public void onChange() {\n                //userList is now filled with data\n                //update UI here\n            }\n        });\n```\n\n```java\n  realm.executeTransaction(new Realm.Transaction() {\n            @Override\n            public void execute(Realm bgRealm) {\n                bgRealm.copyToRealmOrUpdate(user);\n            }\n        });\n```\n\n - However while you working with a small amount of data, don't be afraid to write/load data on UI thread. \n See according [Christian Melchior](http://stackoverflow.com/users/1389357/christian-melchior) answer on [StackOverflow](http://stackoverflow.com/questions/27805580/realm-io-and-asynchronous-queries).\n\n\n*Controlling the lifecycle of Realm instances*\n\n - To avoid the overhead of opening and closing the Realm data\n   connection, Realm has a reference counted cache internally. This\n   implies that calling `Realm.getDefaultInstance()` multiple times on the\n   same thread is free, and the underlying resources will automatically\n   be released when all instances are closed.\n   \n   This means that on the UI thread the easiest and safest approach is\n   to open a Realm instance in all your Activities and Fragments and\n   close it again when the `Activity` or `Fragment` is destroyed.\n\n```java\npublic class RealmApplication extends Application {\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        RealmConfiguration realmConfiguration = new RealmConfiguration\n        .Builder(this).build();\n        Realm.setDefaultConfiguration(realmConfiguration);\n    }\n}\n```\n```java\npublic class RealmActivity extends AppCompatActivity {\n\n    protected Realm mRealm;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        mRealm = Realm.getDefaultInstance();\n    }\n\n    @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        if (mRealm != null) {\n            mRealm.close();\n        }\n    }\n}\n```\n*Reuse RealmResults and RealmObjects*\n\n - On the UI thread and all other Looper threads, all `RealmObjects` and\n   `RealmResults` are automatically refreshed when changes are made to the\n   Realm. This means that it isn’t necessary to fetch those objects\n   again when reacting to a `RealmChangedListener`. The objects are\n   already updated and ready to be redrawn on the screen.\n\n**Keep in mind while using Realm (Limitations)**\n\n`RealmObject` can have:\n\n - Only private instance fields.\n - Only default getter and setter methods.\n - Static fields, both public and private.\n - Static methods.\n - Implementing interfaces with no methods.\n\nYou can only save objects that extend `RealmObject` inside a Realm.\nThat means that you have to declare an `RealmList` if you want to save a List, or extend `RealmObject` while saving an object. \n\n\n\nSee [StackOverflow](http://stackoverflow.com/questions/30097810/listobject-or-realmlistrealmobject-on-realm-android) answer for data saving.\n\n**More Info**\n\nSee [Realm documentation](https://realm.io/docs/java/latest/) for more details\n\n**License**\n\n    The MIT License (MIT)\n    \n    Copyright (c) 2015 Lemberg Solutions\n    \n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n    \n    The above copyright notice and this permission notice shall be included in all\n    copies or substantial portions of the Software.\n    \n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n    SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemberg%2Fandroid-realm-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemberg%2Fandroid-realm-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemberg%2Fandroid-realm-sample/lists"}