Realm-java: I want to store an array of strings in Realm

Created on 19 Jan 2018  路  5Comments  路  Source: realm/realm-java

Hi,

I am wanting to store an array of strings in realm through my object.Although I am new to realm but i have read through the documentation completely and also tried out samples. Here is my example model.From what i have gathered so far on stackoverflow that this is not possible to store an array of strings inside an object we are putting in realm? Is my understanding and findings correct?

package com.example.fahdsaif.realmtest;

import java.util.ArrayList;
import java.util.List;

import io.realm.RealmObject;

/**

  • Created by fahdsaif on 17/1/18.
    */

public class RealmModel extends RealmObject {
private String firstName;
private String lastName;
private String meraarray[] = new String[10];

public String[] getMeraarray() {
    return meraarray;
}

public void setMeraarray(String[] meraarray) {
    this.meraarray = meraarray;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

}

and here is the realm code for inserting into this model:

some code then...

myrealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgrealm) {
RealmModel myrealmModel = bgrealm.createObject(RealmModel.class);
//INSERTION
myrealmModel.setFirstName("aik aur stable");
myrealmModel.setLastName("aik aur kam stable");
myrealmModel.setMeraarray(meraarraykavalues);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
Toast.makeText(getApplicationContext(), "There was an error"+error.toString(), Toast.LENGTH_SHORT).show();
Log.d("kuchbhe", error.toString());
}
});

some other code...

Error:(12, 8) error: Field "meraarray" of type "java.lang.String[]" is not supported.Warning:Unclosed files for the types '[io.realm.RealmModelRealmProxy]'; these types will not undergo annotation processing

T-Help

Most helpful comment

Use RealmList<String> instead of String[].

All 5 comments

Did you see this: https://realm.io/docs/java/latest#lists-of-primitives?

Please list the SO questions. They are likely old and we should update the answers. Also let us know how we can make it more discoverable in the docs!

Perhaps we should have it as an example in the first model we show under "Models".

Thanks for your response, Ill go over through the link https://realm.io/docs/java/latest#lists-of-primitives. here are some of the links that I have been searching through:
1) https://stackoverflow.com/questions/42361281/how-to-store-array-of-strings-into-realm-instance-using-a-dictionary?rq=1

2)https://github.com/realm/realm-cocoa/issues/3800

3)https://github.com/realm/realm-cocoa/issues/2318#issuecomment-126392191

Use RealmList<String> instead of String[].

@FahdSaif Thanks for those links! I've now posted updates to all 3. Let me know if you can find more :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wezley98 picture wezley98  路  3Comments

nolanamy picture nolanamy  路  3Comments

gpulido picture gpulido  路  3Comments

aschrijver picture aschrijver  路  3Comments

pawlo2102 picture pawlo2102  路  3Comments