Search code examples
javaandroidormlite

Android Ormlite getting SelectorIterator<T, ID> instead of CloseableIterator<T>


I am trying out android Ormlite but getting this error:

com.j256.ormlite.stmt.SelectIterator cannot be cast to
       com.j256.ormlite.dao.CloseableIterable

This is the code causing the error; instead of CloseableIterator<Employee> it is returning SelectorIterator<Employee>:

public CloseableIterator<Employee> getEmployeesToSync(Context context){
    DatabaseHelper databaseHelper = new DatabaseHelper(context);
    CloseableIterator<Employee> iterator = null;
    try {
        iterator = databaseHelper.getEmployeesDao().queryBuilder().
            where().
            eq(Employee.IS_SYNCED, false).
            iterator();
    } catch (Exception ex) {
        Log.e(LOG_TAG, ex.getMessage());
    }
    return iterator;
}

Solution

  • com.j256.ormlite.stmt.SelectIterator cannot be cast to
       com.j256.ormlite.dao.CloseableIterable
    

    I'm not 100% sure why this is happening unless the Employee type is a different class. I'd make sure that your code is referring to the same class by looking at the import statements.

    This shouldn't be a casting issue because SelectIterator<T, ID> extends CloseableIterable<T>, and Where.iterator() returns a CloseableIterator which happens to be a SelectIterator. See the SelectIterator javadocs.

    I'm not sure if this is the problem but you will need to cast a CloseableIterator<T> to be a SelectIterator<T, ID> because there is no way for the generics to infer the ID type. So if you have a CloseableIterator<Employee> and the id type of Employee is an int then you will need to case it to be a SelectIterator<Employee, Integer>.