我可以编译代码:
@Update
public abstract int update(Iterable<T> objects);
但是当我尝试编译时
@Insert(onConflict = OnConflictStrategy.IGNORE)
public abstract List<Long> insert(Iterable<T> objects);
有来自编译器的消息:
error: no suitable method found for insertAndReturnIdsList(Iterable<SessionKey>)
method EntityInsertionAdapter.insertAndReturnIdsList(SessionKey[]) is not applicable
(argument mismatch; Iterable<SessionKey> cannot be converted to SessionKey[])
method EntityInsertionAdapter.insertAndReturnIdsList(Collection<? extends SessionKey>) is not applicable
(argument mismatch; Iterable<SessionKey> cannot be converted to Collection<? extends SessionKey>)
我发现androidx.room.EntityInsertionAdapter
需要Collection
作为参数:
List<Long> insertAndReturnIdsList(Collection<? extends T> entities)
而不是Iterable
。 为什么?
首先,为什么不能插入:关于Java的“继承”和“多态”`Collection`是`Iterable`的儿子您可以传递`Collection`子类实例,但不能传递`Iterable`
第二,为什么可以更新在EntityDeletionOrUpdateAdapter
中,源代码是
public final int handleMultiple(Iterable<? extends T> entities) {
final SupportSQLiteStatement stmt = acquire();
try {
int total = 0;
for (T entity : entities) {
bind(stmt, entity);
total += stmt.executeUpdateDelete();
}
return total;
} finally {
release(stmt);
}
}
参数是Iterable
不是Collection