Devdroid
Devdroid

Reputation: 941

GreenDao update not working

I use GreenDao in my project and it does a good job for mapping my server model to the android device. What I have been struggling with for a while now is that dao.update and/or dao.updateInTx methods are not updating any rows in the database.

What I do:

/**
 *  The first approach -> All in one runnable
 *
 */

// list definitions
ArrayList<Country> countryList;
ArrayList<Country> countryListDetail;  

final DaoSession daoSession = daoMaster.newSession();

// execute everything in runnable 
// in order to optimize insert time
daoSession.runInTx(new Runnable() {

    @Override
    public void run() {
        CountryDao countryDao = new CaountryDao();


                // delete all records first
                countryDao.deleteAll();

                // insert countries with less data
                size = countryList.size(); 
                for (int i=0; i < size; i++) {
                     countryDao.insert(countryList.get(i));
                }

                // update countries with more data
                // ID's of the Objects in countryListDetail match those from
                // the countryList, so the primary key mathces
                size = countryListDetail.size(); 
                for (int i=0; i < size; i++) {
                     countryDao.update(countryListDetail.get(i));
                }

      }
}

/**
 *  The second approach -> updateInTx()
 *
 */

// list definitions
ArrayList<Country> countryList;
ArrayList<Country> countryListDetail;              

// insert & update logic
final DaoSession daoSession = daoMaster.newSession();                                
CountryDao countryDao = new CaountryDao();

countryDao.insertInTx(countryList);
countryDao.updateInTx(countryListDetail);

In both cases, when I pull the database from the device and inspect it, the Country table has only the base insert data, but no detail data which should come from the update statements. When debugging, the GreenDao logic seems to execute the updateInsideSynchronized() method and stmt.execute() is also called.

Does anybody know what I may be doing wrong?

Upvotes: 2

Views: 4421

Answers (2)

kukku
kukku

Reputation: 489

Please enable this and check

dao.queryBuilder().LOG_VALUES=true;
dao.queryBuilder().LOG_SQL=true;

and make sure countryList and countryListDetail have the same Primarykey when you update

Upvotes: 2

AlexS
AlexS

Reputation: 5345

Probably your countyListDetail doesn't contain Country-objects, that have been queried using greendao. Hence the objects have "no idea about" the database, because there is no reference to the DaoSession. The update only works for objects already inserted or queried from the database.

If the data in countryList is included in the data of countryListDetail, I'd use insertOrReplace.

Otherwise you have to merge your Country-objects before insert or before update.

Upvotes: 1

Related Questions