Gson deserialization of List into realmList

It is better to use JsonSerializer and JsonDeserializer rather than TypeAdapter for your RealmObject, because of 2 reasons: They allow you to delegate (de)serialization for your RealmObject to the default Gson (de)serializer, which means you don’t need to write the boilerplate yourself. There’s a weird bug in Gson 2.3.1 that might cause a StackOverflowError during … Read more

How to filter the data in realm adapter?

public void setFilter( List<RealmPhoneCallLogs> filtedData) { filter = new ArrayList<>(); // <– WRONG filter.addAll(filtedData); // <– WRONG private List<RealmPhoneCallLogs> filter(List<RealmPhoneCallLogs> models, String query) { query = query.toLowerCase(); final List<RealmPhoneCallLogs> filteredModelList = new ArrayList<>(); // <– WRONG for (RealmPhoneCallLogs model : models) { // <– WRONG final String text = model.getNumber().toLowerCase(); if (text.contains(query)) { // <– … Read more

Android: Realm + Retrofit 2 + Gson

Why writing all these custom serializers when you can make Gson and Realm work together with just ONE LINE OF CODE? TL;DR. You can simply solve this by passing unmanaged RealmObjects to your Retrofit calls. MyModel model = realm.where(MyModel.class).findFirst(); MyModel unmanagedModel = realm.copyFromRealm(model); // then pass unmanagedModel to your retrofit calls If you don’t want … Read more

Realm with pre populated data into assets?

Since Realm Java 0.91.0 there has been an assetFile(String) option on the RealmConfiguration that automatically will copy a file from assets and use that if needed (e.g. if the Realm is opened the first time or has been deleted for some reason): RealmConfiguration config = new RealmConfiguration.Builder() .assetFile(“path/to/file/in/assets”) // e.g “default.realm” or “lib/data.realm” .deleteRealmIfMigrationNeeded() .build() … Read more

How to put an image in a Realm database?

You can store images as NSData. Given you have the URL of an image, which you want to store locally, here is a code snippet how that can be achieved. class MyImageBlob { var data: NSData? } // Working Example let url = NSURL(string: “http://images.apple.com/v/home/cb/images/home_evergreen_hero_iphone_medium.jpg”)! if let imgData = NSData(contentsOfURL: url) { var myblob = … Read more

How to read contacts in Android using Realm?

Create RealmObject, read the data from content provider, save data to RealmObject, save data in Realm: // background thread Realm realm = null; try { realm = Realm.getDefaultInstance(); realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { RealmContact realmContact = new RealmContact(); Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (phones.moveToNext()) { String name … Read more

Upgrade realm in an Android project

The list of breaking changes is available in the CHANGELOG.MD on their Github. However, it’s worth noting that there were quite a few breaking changes on the road, especially noting 0.89.0. From 0.82.0 to 5.1.0 is the following (which is the most stable version at the moment): 0.82.0: BREAKING CHANGE: Fields with annotation @PrimaryKey are … Read more