为什么Room插入方法不接受Iterable?

问题描述 投票:-1回答:1

我可以编译代码:

@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为什么?

android collections insert android-room iterable
1个回答
1
投票

首先,为什么不能插入:关于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

© www.soinside.com 2019 - 2024. All rights reserved.