Chris Legge
Chris Legge

Reputation: 789

Realm not saving (or possibly not returning) String values of related object

Realm not saving (or possibly not returning) String values of related object...

i have 3 models:

public class Customer extends RealmObject {
@Expose
@PrimaryKey
private Long id;
@Expose
private Long historicalId;
@Expose
private String versionUUID;
@Expose
private String nameCompany;
@Expose
private String email;
@Expose
private String phoneNumber;
@Expose
private String notes;
@Expose
private boolean active;
@Expose
private boolean currentVersion;
@Expose
private Date lastUpdated;
@Expose
private Date dateCreated;

public Customer() {
}

and

public class Project extends RealmObject {
@PrimaryKey
private Long id;
private Long historicalId;
private String versionUUID;
private String name;
private String description;
private String addressLineOne;
private String addressLineTwo;
private String addressCity;
private String addressState;
private String addressZip;
private String notes;
private Date lastUpdated;
private Date dateCreated;
private boolean active;
private boolean currentVersion;
private Customer customer;
private String customerVersion;

public Project() {
}

and lastly (added for the comment question)

public class Receipt extends RealmObject {
@PrimaryKey
private String id;
private String name;
private String vendor;
private Double amount;
private String description;
private Date dateCreated;
private Date lastUpdated;
private Date dateSynced;
private byte[] imageByteArray;
private Project project;
private String projectVersion;
private int imgWidht;
private int imgHeight;

public Receipt() {
}

i am saving the data via:

    public static void syncAllDataToRealm(Context context){
    Globals globals = Globals.getInstance();
    Realm realm = Realm.getInstance(context);
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(globals.getAllCustomers());
    realm.copyToRealmOrUpdate(globals.getAllProjects());
    realm.commitTransaction();
    testRealCommit(context);
}

and i am verifying the data via

    private static void testRealCommit(Context context){
    Realm realm = Realm.getInstance(context);
    RealmQuery<Customer> customerRealmQuery = realm.where(Customer.class);
    RealmResults<Customer> customerRealmResults = customerRealmQuery.findAll();
    logger.debug(LogUtility.generateMessage(TAG, "===== CUSTOMER ======= "));
    for(Customer c: customerRealmResults){
        logger.debug(LogUtility.generateMessage(TAG, c.getId() + " - " + c.getNameCompany()));
    }
    logger.debug(LogUtility.generateMessage(TAG, "===== CUSTOMER  GLOBAL======= "));
    for(Customer c: Globals.getInstance().getAllCustomers()){
        logger.debug(LogUtility.generateMessage(TAG, c.getId() + " - " + c.getNameCompany()));
    }


    RealmQuery<Project> projectRealmQuery = realm.where(Project.class);
    RealmResults<Project> projectRealmResults = projectRealmQuery.findAll();
    logger.debug(LogUtility.generateMessage(TAG, "===== PROJECT ======="));
    for(Project p: projectRealmResults){
        logger.debug(LogUtility.generateMessage(TAG, p.getId() + " - " + p.getName()));
    }
}

for some reason:

c.getNameCompany()

returns a null in the above code... if i dont add the project data to realm it works fine....

realm is bein set up in my Application file via:

        RealmConfiguration config = new RealmConfiguration.Builder(context)
            .name("receiptbucket.realm")
            .schemaVersion(2)
            .build();

    Realm.setDefaultConfiguration(config);

any ideas???

found out something else... if i swap the commit order, adding all customers after adding all projects it works

    realm.copyToRealmOrUpdate(globals.getAllProjects());
    realm.copyToRealmOrUpdate(globals.getAllCustomers());

short term fix but i would like to know why i have to do it this way for the customer data to stick...

(New Issue)

now when i call copyOrUpdate for the Receipt it wipes all the customer data Projects Customer....

Upvotes: 0

Views: 1683

Answers (2)

Chris Legge
Chris Legge

Reputation: 789

i was able to overcome the last error by querying realm and reattaching the Customer that was getting nulled out to the Project which is part of the Receipt....

so then i started playing.... I was loading realm from Global data... I stopped that and loaded realm right when i got the data from my rest service...

upon doing that everything started just working correctly, lol...

Upvotes: 0

beeender
beeender

Reputation: 3575

From your last description, I think the problem is the Project list returned by globals.getAllProjects() contains some Customer which has null value for nameCompany .

The reason is your Customer class has an id which is annotated with @PrimaryKey, when realm.copyToRealmOrUpdate(globals.getAllProjects()) called, Realm will create or update related objects recursively. (That is the whole point of update here).

If it finds a customer which has the same id and already saved in the Realm, it will just use all new values from Project.customer to update the one existed in the Realm. If the Procject.customer.nameCompany is null, you will have the problem you described above.

The solution would be make the globals.getAllProjects() return the latest value you want to update, since there is no way for Realm to understand whether the null values are something you want to ignore or update to.

Upvotes: 1

Related Questions