在@FTS4 Room 数据库中设置唯一索引会导致构建错误

Setting unique indices in @FTS4 Room database causes build error

我试图通过定义索引使我的 Room 数据库的列唯一。但是,这导致我的编译器失败,因为它大大增加了所需的对象堆。

如果我用@Entity(tableName = "seeds", indices = {@Index(value = {"name"}, unique = true)}) @Fts4

当 运行 "gradlew build --stacktrace":

时出现编译错误
Error occurred during initialization of VM
Could not reserve enough space for 3145728KB object heap

如果我只使用 @Entity(tableName = "seeds") @Fts4,应用程序可以正确编译。

我在 gradle.properties...

中尝试了不同的设置

org.gradle.jvmargs=-Xmx3g 是我能给它的最大值。在 4g 它抱怨该值超过了允许的最大值。因此,与此相关的所有其他 SO 线程都没有帮助,因为我已经达到了最大值。我通常在 2g 有它。所以这个“小”变化似乎使所需的对象堆增加了一倍。

有谁知道处理唯一索引的更好方法吗?

有谁知道如何解决这个级别的对象堆问题?

正如@CommonsWare 所述,@FTS4 表似乎不支持索引。

在网站上 https://developer.android.com/training/data-storage/room/defining-data 他们提到:

If your app must support SDK versions that don't allow for using FTS3- or FTS4-table-backed entities, you can still index certain columns in the database to speed up your queries.

我现在假设,它们不支持 FTS3/4..

上的索引

我现在通过一种变通方法解决了唯一性问题,即在插入新对象之前检查列中是否存在匹配项:

/**
 * Add a SeedEntity object to the database,
 * if there is no entity in the database with the same name (non-case-sensitive)
 *
 * @param seedEntity Object that is supposed to be added to the database
 * @return  whether the Object has been added or not
 */
public boolean addSeed(SeedEntity seedEntity)
{
    for (SeedEntity entity : mObservableSeeds.getValue())
        if (entity.getName().toLowerCase().equals(seedEntity.getName().toLowerCase()))
            return false;

    mExecutors.diskIO().execute(() ->
            mDatabase.runInTransaction(() ->
                    mDatabase.seedDao().insert(seedEntity)
            )
    );
    return true;
}

不完全是我想要的,但现在解决了这个问题。