Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ormlite SQLiteCursor: close cursor on null twice or more

I have class Coach which has Collection of ScheduleEntity objects;

public class Coach{

    @ForeignCollectionField(columnName = FIELD_SCHEDULE_ENTITY)
    private Collection<ScheduleEntity> scheduleEntities;
}

[...]

public class ScheduleEntity {

    @DatabaseField(columnName = FIELD_COACH, foreign = true)
    private Coach coach;
}

I want to retrieve first ScheduleEntity for coach.

When I do it using QueryBuilder:

public ScheduleEntity getFirstScheduleForCoach(Coach coach) throws SQLException {
    QueryBuilder<ScheduleEntity, Long> queryBuilder = scheduleEntityDao.queryBuilder();
    queryBuilder
            .where()
            .eq(ScheduleEntity.FIELD_COACH, coach);
   return scheduleEntityDao.queryForFirst(queryBuilder.prepare());

Some weird info appears in logcat:

Close cursor android.database.sqlite.SQLiteCursor@42321aa0 on null twice or more

However when I do it in another way:

return scheduleEntityDao.queryForEq(ScheduleEntity.FIELD_COACH, coach).iterator().next();

Everything is fine (don't care about possible nullpointer).

What does that information mean? Am I doing something wrong or is it just normal behaviour?

like image 949
Than Avatar asked Aug 11 '14 14:08

Than


1 Answers

I went ahead downloaded ORMLite's code.

in the AndroidCompiledStatement class on row 89, i changed the close method as follows:

before

public void close() throws IOException {
    if (cursor != null) {
        try {
            cursor.close();
        } catch (android.database.SQLException e) {
            throw new IOException("Problems closing Android cursor", e);
        }
    }
    cancellationHook = null;
}

after

public void close() throws IOException {
    if (cursor != null && !cursor.isClosed()) {
        try {
            cursor.close();
        } catch (android.database.SQLException e) {
            throw new IOException("Problems closing Android cursor", e);
        }
    }
    cancellationHook = null;
}

simply checking that if the cursor is already closed pretty much fixed this for me, though you'd have to clone the ormlite-android git into your project.

like image 142
nadavfima Avatar answered Oct 23 '22 07:10

nadavfima