1

I made this post because I don't understand how transaction works using GreenDao and I would love to get some explanation/recent tutorial.

Despite of reading the Greendao github, I don't understand how the main activity works. For example:

...
    Note note = new Note(null, noteText, comment, new Date(), NoteType.TEXT);
    noteDao.insert(note)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<Note>() {
                @Override
                public void call(Note note) {
                    Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
                    updateNotes();
                }
            });
...

Do the observeOn method sends every insertion/update to one limited buffer? so, it will manage automatically the insertion to the real database when every operation was finished whithout problems?

Reading its API, we could see that they use rxTx and rxTxPlain for transactions (other way?), and finding out where are performed, I have to say I misunderstood everything. For example:

...
    public void testConcurrentInsertDuringTx() throws InterruptedException {

        ...

        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                dao.insertInTx(createEntity(null));
            }
        };
        Runnable runnable3 = new Runnable() {
            @Override
            public void run() {
                daoSession.runInTx(new Runnable() {
                    @Override
                    public void run() {
                        dao.insert(createEntity(null));
                    }
                });
            }
        };

        ...           

        // Builds the statement so it is ready immediately in the thread
        dao.insert(createEntity(null));
        doTx(new Runnable() {
            @Override
            public void run() {
                dao.insert(createEntity(null));
            }
        });
        latchThreadsDone.await();
        assertEquals(7, dao.count());
    }
...

I know this is an example, but I don't know the diferences of using the rx methods and the observeOn/subscribe ones

protected void doTx(final Runnable runnableInsideTx) {
    daoSession.runInTx(new Runnable() {
        @Override
        public void run() {
            latchInsideTx.countDown();
            // Give the concurrent thread time so it will try to acquire locks
            try {
                Thread.sleep(TIME_TO_WAIT_FOR_THREAD);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            runnableInsideTx.run();
        }
    });
}

I read some topics about transactions, but they are obsoletes or maybe, I didn't understand them correctly:

SQLite transaction behaviour using greendao

Android - Green Dao Multiple Transaction

Transactions in GreenDao

greenDAO 40 seconds to insert 600 records

https://groups.google.com/forum/#!topic/greendao/PXRwCRTbP6c

Someone could help me giving me some explanation/recent tutorial? Greendao have these methods as experimentals. Thanks in advance

Community
  • 1
  • 1
Jaco
  • 923
  • 2
  • 14
  • 28

0 Answers0