Realm-java: Unable to enter values into table

Created on 28 Nov 2016  路  25Comments  路  Source: realm/realm-java

Goal

To Enter Values in Tables

Expected Results

Database should contain the value

Actual Results

Database does not renders correct value

Steps & Code to Reproduce

Used accessor method to see the values entered in Database. The values are entered correctly but when taken the realm file out to view in realm browser the values entered are 0.

Code Sample

  for (int j = 0; j < syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().size(); j++) {

                                                    SweatPointFitnessDB sweatPointFitnessDB = realm.where(SweatPointFitnessDB.class)
                                                            .equalTo("forday", syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getDate())
                                                            .findFirst();

                                                    String dateSplit[] = syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getDate().split("-");
                                                    currentMonthNo = dateSplit[1];
                                                    Calendar currentTime = Calendar.getInstance();
                                                    currentTime.set(Calendar.MONTH, Integer.valueOf(dateSplit[1]) - 1);
                                                    currentMonth = nameFormat.format(currentTime.getTime()).toString();




                                                    if (sweatPointFitnessDB == null) {

                                                        sweatPointFitnessDB = realm.createObject(SweatPointFitnessDB.class, syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getDate());

                                                    }


                                                    sweatPointFitnessDB.setGoalSweatPoints(syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getGoalProgressSweatPoints());
                                                    sweatPointFitnessDB.setBadgeSweatPoints(syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getBadgeSweatPoints());
                                                    sweatPointFitnessDB.setSweatpoints(syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getFitnessSweatPoints());
                                                    sweatPointFitnessDB.setRewardSweatPoints(syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getRewardSweatPoints());
                                                    sweatPointFitnessDB.setBonusSweatPoints(syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getBonusSweatPoints());
                                                    sweatPointFitnessDB.setDayTotalSweatPoints(syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j).getDayTotalSweatPoints());
                                                    sweatPointFitnessDB.setCurrentMonth(currentMonth);
                                                    sweatPointFitnessDB.setCurrentMonthNo(currentMonthNo);
                                                    realm.copyToRealmOrUpdate(sweatPointFitnessDB);

                                                    Log.i(TAG,"SP DAY: "+sweatPointFitnessDB.getForday());
                                                    Log.i(TAG,"SP TOTAL: "+sweatPointFitnessDB.getDayTotalSweatPoints());


                                                }

P.S Uploading 2 screenshot 1st) Android studio console, 2nd) Realm Browser
screenshot 2016-11-28 15 16 22
screenshot 2016-11-28 15 18 28

Version of Realm and tooling

Realm version(s): io.realm:realm-gradle-plugin:2.1.1

Realm sync feature enabled:no

Android Studio version: 2.2

Which Android version and device: Nexus 6p

T-Help

Most helpful comment

@Sutirth he says you should copy out your Realm file from your application, put it in another project as an asset file with configBuilder.assetFile(), use the same Realm schema for it (so copy over your entities to the new project), and then try writing into the Realm file directly, and see if it still writes 0 in the values.

All 25 comments

....this honestly feels like dejavu, because I specifically remember saying that syncApiModel.getData().get(entityNum).getSweatPoints().getUpdate().get(j) should be replaced with a local variable in order to make the code more readable.

Have you debugged whether you truly insert into Realm what you expect to see in the Realm Browser?

@Zhuinden Yes I have debugged it as well and have check through accessor method as well reference 1st screenshot I get values for dates here and refer 2nd screenshot the last column dayTotalSweatPoints
almost all the values are 0's

We have many unit tests to save values into database and no tests failed at all.

Could you make a simple test that shows it's not a program error?

@zaki50 I am not saying the entire values are failing some are being inserted while some are being not. I am afraid the unit test would provide result as all the values are passing but I am unable to execute this in real scenario

I am afraid the unit test would provide result as all the values are passing

Then this is an error in either your JSON input, or user error

@Zhuinden But Then I am getting all the desired values from json and more over you have seen the code what might be the error then??

@Sutirth we need a code that reproduces the issue to fix.

We have many tests to save multiple values and no test reproduces this.

@zaki50 So any pointers for me how to proceed ahead???

Could you create a simple app that reproduce this issue?

@zaki50 Okay would create a sample application and see if I can reproduce the issue

@zaki50 So I pulled out the modules and made a sample app. As expected all the desired values and fields were added. A note in my prod app the fields were added in migration is there vaguely a possibility that somehow my table is corrupted. Should I create a new table for same and migrate my data into that??

No, this means there's probably an error in your prod app which was not migrated over to the sample app.

@Zhuinden Response is same from json, table structure is same as well. Any pointers from your side to pin point the issue

@Sutirth Does your app work as expected when you uninstall and then install again?
It creates latest schema without migration.

@zaki50 I always did migration and then move forward never uninstall and installed it again since this wont be scenario with our user in production app

@Sutirth But we need to know this issue is caused by migration, don't we?

@zaki50 I agree. But i am already way ahead in database version migration number currently from last time when I migrated this particular table. So as to I dont know how should I check it now?? I had tried couple of times earlier in production to completely uninstall and do fresh install of application on migration it worked perfectly then.

How about writing a code that simply insert a SweatPointFitnessDB object that has all hard coded values into migrated database file.

@zaki50 You mean write code in migration file for e.g

if (oldVersion == 23) {
            RealmObjectSchema sweatPointFitnessDBSchema = schema.get("SweatPointFitnessDB")
                    .addField("dayTotalSweatPoints", int.class);


            oldVersion++;
        }

Hard code some values here??

@Sutirth he says you should copy out your Realm file from your application, put it in another project as an asset file with configBuilder.assetFile(), use the same Realm schema for it (so copy over your entities to the new project), and then try writing into the Realm file directly, and see if it still writes 0 in the values.

@zaki50 I did what you said and apparently all the hard coded values are inserted into the schema and can be retrieved as well

@Sutirth I think it means that this issue is happening by the error in your code or data.

@zaki50 hmm will making a new table and migrating old data into it will help resolve this issue??

I don't think so.

@zaki50 Thanks for the help.

Was this page helpful?
0 / 5 - 0 ratings