accessing kotlin Suspend function from Java code - android

I have Room database interface which is a Kotlin file and as I dont want the calls to run on mainthread I am using kotlin Suspend. how can I use suspend function from java
I have two method where I want to insert a User and another to retrieve the users
these are the errors I get in my java file
Code
Room Dao interface
UserDao
#Dao
interface UserDao {
fun getAllUsersAsync(): CompletableFuture<List<User>> =
GlobalScope.future { getAllUsers() }
#Query("SELECT * FROM user")
suspend fun getAllUsers(): List<User>
#Insert
suspend fun insertUser(user: User): Long
}
Java code
private Long addUser(com.i6systems.in2plane.AppDatabase.User user) {
return userDao.insertUser(user);
}
private List<com.i6systems.in2plane.AppDatabase.User> getUsers () {
return userDao.getAllUsersAsync();
}
your help is much appreciated
Thanks
R

Related

DAO when to use suspend function android

I am following DAO tutorial on Android developers here:
https://developer.android.com/codelabs/android-room-with-a-view-kotlin#5
They say:
By default, all queries must be executed on a separate thread.
Room has Kotlin coroutines support. This allows your queries to be annotated with the suspend modifier and then called from a coroutine or from another suspension function.
Dao interface is as follows:
#Dao
interface WordDao {
#Query("SELECT * FROM word_table ORDER BY word ASC")
fun getAlphabetizedWords(): List<Word>
#Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(word: Word)
#Query("DELETE FROM word_table")
suspend fun deleteAll()
}
Why getAlphabetizedWords() is not defined as suspend function?
In coroutines, a flow is a type that can emit multiple values sequentially, as opposed to suspend functions that return only a single value. For example, you can use a flow to receive live updates from a database.
#Dao
interface WordDao {
// The flow always holds/caches latest version of data. Notifies its observers when the
// data has changed.
#Query("SELECT * FROM word_table ORDER BY word ASC")
fun getAlphabetizedWords(): Flow<List<Word>>
#Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(word: Word)
#Query("DELETE FROM word_table")
suspend fun deleteAll()
}
you can see source code in Github.

Room Dao inheritance

Is is it possible to have a Dao with a non annotated method? And override it in the derived classes?
interface DaoBase<TEntity, TId> where TEntity : Entity<TId> {
#Insert
fun add(entity: TEntity)
fun get(id: TId): TEntity?
#Update
fun update(entity: TEntity)
}
fun <TEntity, TId> DaoBase<TEntity, TId>.addOrUpdate(entity: TEntity) where TEntity : Entity<TId> {
val entityQ = get(entity.id)
if(entityQ == null) {
add(entity)
} else {
update(entity)
}
}
I get this error for the DaoBase object:
DaoBase.java:13: error: An abstract DAO method must be annotated with one and only one of the following annotations: Insert,Delete,Query,Update,RawQuery
public abstract TEntity get(TId id);
And the same error on any Dao that inherits from it. I think the code that generates the code may have a bug?
add #Query above get
#Query("SELECT * FROM tableName WHERE id=:id")
fun get(id: TId): TEntity?
Also you can forgo your addOrUpdate function, and your update function
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun add(entity: TEntity)
which is the same as updating

Obtain entity using coroutines api

What is the best way to use coroutines with LiveData for selecting some data from database using Room.
This is My Dao class with suspended selection
#Dao
interface UserDao {
#Query("SELECT * from user_table WHERE id =:id")
suspend fun getUser(id: Long): User
}
Inside of View Model class I load user with viewModelScope.
Does it correct way to obtain user entity ?
fun load(userId: Long, block: (User?) -> Unit) = viewModelScope.launch {
block(dao.getUser(userId))
}
According developer android mentioned
val user: LiveData<User> = liveData {
val data = database.loadUser() // loadUser is a suspend function.
emit(data)
}
This chunk of code does not work
Your Room must return LiveData.
Use instead:
#Dao
interface UserDao {
#Query("SELECT * from user_table WHERE id =:id")
fun getUser(id: Long): LiveData<User>
}

How return completable on room transaction Android

I need your help please.
I have dao interface that save some configurations:
#Dao interface ConfigDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(config: Config)
#Update(onConflict = OnConflictStrategy.REPLACE)
fun update(config: Config)
#Query("select * from T_CONFIG where isSelected = :isSelected")
fun getConfig(isSelected: Boolean): Single<Config>
#Query("select * from t_config")
fun getConfigAll(): LiveData<MutableList<Config>>
#Query("update T_CONFIG set isSelected = :isSelected where idEnvironment = :id")
fun updateConfigById(id: String, isSelected: Boolean):Completable
#Transaction
fun updateConfigTransaction(configSelected: Config){
if (configSelected.idEnvironment == Environtment.Type.PRD.toString()){
updateConfigById(Environtment.Type.PRD.toString(), false)
updateConfigById(Environtment.Type.DEV.toString(), true)
}else{
updateConfigById(Environtment.Type.PRD.toString(), true)
updateConfigById(Environtment.Type.DEV.toString(), false)
}
}
}
I need to know when the transaction is complete with success or error.
I tried to implement Completable from io.reactivex but it's not possible.
Since Room 2.1.0
Additional Async Support: DAO methods annotated with #Insert, #Delete or #Update, along with #Query containing INSERT, DELETE or UPDATE statements, now support Rx return types Completable, Single, Maybe, and Guava's return type ListenableFuture, and they can also be suspend functions.
Source: https://developer.android.com/jetpack/androidx/releases/room#2.1.0
Older versions
Change the interface to an abstract class. You'll have to prefix all methods without implementation with abstract. Then:
abstract class ConfigDao(private val db: MyDatabase) {
private val scheduler = Schedulers.from(db.queryExecutor)
// Make sure the method is open so Room can generate the transaction handling code.
#Transaction
open fun updateConfigTransaction(configSelected: Config){
// ...
}
fun updateConfigTransactionAsync(configSelected: config): Completable {
return Completable
.fromAction { updateConfigTransaction(config) }
.subscribeOn(scheduler)
}
}
subscribeOn(db.queryExecutor) makes sure the query runs on the same thread as all other DAO methods returning RxJava types. Replace MyDatabase constructor parameter with whatever your database class is.

Reusable generic base class DAOs with Android Room

Is there any way to create reusable generic base class DAOs with Android Room?
public interface BaseDao<T> {
#Insert
void insert(T object);
#Update
void update(T object);
#Query("SELECT * FROM #{T} WHERE id = :id")
void findAll(int id);
#Delete
void delete(T object);
}
public interface FooDao extends BaseDao<FooObject> { ... }
public interface BarDao extends BaseDao<BarEntity> { ... }
I haven't been able to figure out any way of achieving this without having to declare the same interface members and write the query for each sub class. When dealing with a large number of similar DAOs this becomes very tedious...
Today, August 08, 2017, with version 1.0.0-alpha8 the Dao below works. I can have other Dao heroing the GenericDao.
#Dao
public interface GenericDao<T> {
#Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(T... entity);
#Update
void update(T entity);
#Delete
void delete(T entity);
}
However, GenericDao can not be included in my Database class
Generic findAll function:
base repository and dao:
abstract class BaseRepository<T>(private val entityClass: Class<T>) {
abstract val dao: BaseDao<T>
fun findAll(): List<T> {
return dao.findAll(SimpleSQLiteQuery("SELECT * FROM ${DatabaseService.getTableName(entityClass)}"))
}
}
interface BaseDao<T> {
#RawQuery
fun findAll(query: SupportSQLiteQuery): List<T>
}
database service:
object DatabaseService {
fun getEntityClass(tableName: String): Class<*>? {
return when (tableName) {
"SomeTableThatDoesntMatchClassName" -> MyClass::class.java
else -> Class.forName(tableName)
}
}
fun getTableName(entityClass: Class<*>): String? {
return when (entityClass) {
MyClass::class.java -> "SomeTableThatDoesntMatchClassName"
else -> entityClass.simpleName
}
}
}
example repo and dao:
class UserRepository : BaseRepository<User>(User::class.java) {
override val dao: UserDao
get() = database.userDao
}
#Dao
interface UserDao : BaseDao<User>
I have a solution for findAll.
Codes in that BaseDao:
...
public List<T> findAll() {
SimpleSQLiteQuery query = new SimpleSQLiteQuery(
"select * from " + getTableName()
);
return doFindAll(query);
}
...
public String getTableName() {
// Below is based on your inheritance chain
Class clazz = (Class)
((ParameterizedType) getClass().getSuperclass().getGenericSuperclass())
.getActualTypeArguments()[0];
// tableName = StringUtil.toSnakeCase(clazz.getSimpleName());
String tableName = clazz.getSimpleName();
return tableName;
}
...
#RawQuery
protected abstract List<T> doFindAll(SupportSQLiteQuery query);
and other Dao looks like :
#Dao
public abstract class UserDao extends AppDao<User> {
}
That's all
The idea is
Get the table name of subclass's generic type on runtime
Pass that table name to a RawQuery
If you prefer interface to abstract class, you can try optional method of java 8.
It's not beautiful but worked, as you can see.
I created a gist at here
AFAIK, you can do it only for insert(), update(), and delete(), as it doesn't require specific SQL statement that needs to be verified at compile time.
example:
BaseDao.java
public interface BaseDao<T> {
#Insert
void insert(T obj);
#Insert
void insert(T... obj);
#Update
void update(T obj);
#Delete
void delete(T obj);
}
UserDao.java
#Dao
abstract class UserDao implements BaseDao<User> {
#Query("SELECT * FROM User")
abstract List<User> getUser();
}
source
It should be implemented as following:
interface BaseDao<T : BaseEntity> {
#Insert
fun insert(entity: T)
#Insert
fun insert(vararg entities: T)
#Update
fun update(entity: T)
#Delete
fun delete(entity: T)
fun getAll(): LiveData<List<T>>
}
#Dao
abstract class MotorDao : BaseDao<Motor> {
#Query("select * from Motor")
abstract override fun getAll(): LiveData<List<Motor>>
}
Although I agree with your thinking, the answer is no. For several reasons.
When the fooDao_Impl.java is generated from your #Dao fooDao extends BaseDao<Foo> class, you will be met with a lot of "cannot find class Symbol T" errors. This is due to the method Room uses to generate dao implementations. This is a method that will not support your desired outcome, and is unlikely to change soon (in my opinion, due to type erasure).
Even if this is resolved, Room does not support dynamic #Dao queries, in an effort to prevent SQL injection. This means you can only dynamically insert values into queries, not column names, table names, or query commands. In the example you have you could not use #{T} as it would breach this principle. In theory if the problem detailed in point 1 is resolved you could user insert, delete and update though.

Categories

Resources