I use RealmString alternative to List < String > in Realm Android. But I have a problem with RealmChangeListener. I use RealmString in many models like Dog, Cat, Chicken in below example. But when anyone in that change data, all of them change too.
Ex: When I save Dog -> Dog Change and Cat Change too.
I can fix this problem by use RealmDogString, RealmCatString, RealmChickenString but it duplicates my code. Have anyone better solution?
This is my examples code:
public class RealmTestActivity extends AppCompatActivity {
private Context context;
private Realm realm;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_realm_test);
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build()).build());
context = this;
RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfig);
realm = Realm.getDefaultInstance();
final RealmResults<Dog> puppies = realm.where(Dog.class).lessThan("age", 2).findAll();
puppies.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {
@Override
public void onChange(RealmResults<Dog> results) {
System.out.println("XXX Dog ::" + puppies.size());
Toast.makeText(context, "Dog Change", Toast.LENGTH_SHORT).show();
puppies.size();
}});
final RealmResults<Cat> puppiesCat = realm.where(Cat.class).lessThan("age", 2).findAll();
puppiesCat.addChangeListener(new RealmChangeListener<RealmResults<Cat>>() {
@Override
public void onChange(RealmResults<Cat> results) {
System.out.println("XXX Cat ::" + puppiesCat.size());
Toast.makeText(context, "Cat Change", Toast.LENGTH_SHORT).show();
}
});
}
public void saveDog(View view) {
Dog dog = new Dog();
dog.setName("Bella");
dog.setAge(1);
RealmList<RealmString> realmStrings = new RealmList<RealmString>();
RealmString realmString = new RealmString();
realmString.setValue("link_Bella.png");
realmStrings.add(realmString);
dog.setImage(realmStrings);
realm.beginTransaction();
realm.copyToRealm(dog);
realm.commitTransaction();
}
public void saveCat(View view) {
Cat cat = new Cat();
cat.setName("Oliver");
cat.setAge(1);
RealmList<RealmString> realmStrings_2 = new RealmList<RealmString>();
RealmString realmString_2 = new RealmString();
realmString_2.setValue("link_Oliver.png");
realmStrings_2.add(realmString_2);
cat.setImage(realmStrings_2);
realm.beginTransaction();
realm.copyToRealm(cat);
realm.commitTransaction();
}
}
In this tutorial, we’ll explore the fundamentals of Realm for Android. Android Studio installed on your machine. Good knowledge of creating and running Android applications. Basic information of the Kotlin programming language. A good understanding of MVVM architecture in Android.
To add the Realm library to the project, first add the classpath dependency to the project level build.gradlefile. buildscript {repositories {jcenter()}dependencies {classpath "io.realm:realm-gradle-plugin:3.1.4"}} Then apply the realm-android plugin to the top of the application level build.gradlefile. apply plugin:'realm-android'
It stores data in a native object format whereby objects are represented in the database in a universal table-based format. This makes Realm database easy to use in different languages and platforms. In this tutorial, we will focus on the Realm database in Android with Kotlin.
Realm supports the following field types: boolean, byte, short, int, long, float, double, String, Dateand byte[]as well as the boxed types Boolean, Byte, Short, Integer, Long, Floatand Double. Subclasses of RealmObjectand RealmList<?extendsRealmObject>are used to model relationships.
Realm for Android supports list of primitives from 4.0.0 version: https://github.com/realm/realm-java/issues/575
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With