简体   繁体   English

Android Room,最大数据库容量上限

[英]Android Room, Capping max database size

I am working on a production app, so I am not able to share the code. 我正在开发生产应用程序,因此无法共享代码。 But I will try my best to explain the current scenario. 但是,我将尽力解释当前的情况。

We need to set a maximum size of the database, this size is provided via API call (so this might change at runtime if API provides a new size). 我们需要设置数据库的最大大小,此大小是通过API调用提供的(因此,如果API提供了新的大小,则此大小可能会在运行时更改)。 For example, let's consider the max size as 10 MB. 例如,让我们考虑最大大小为10 MB。 Once the database reaches this maximum size, we need to remove some database entries so that new entries can be inserted. 数据库达到此最大大小后,我们需要删除一些数据库条目,以便可以插入新条目。

Here I am confused a bit. 在这里我有点困惑。 How to set the maximum size of the database when using Android Room and how to figure out that how many entries I need to delete in order to insert a new record and keep the database at its max size allowed. 如何在使用Android Room时设置数据库的最大大小,以及如何确定需要删除多少条目才能插入新记录并保持数据库处于允许的最大大小。

Hope I am clear, but I can provide additional examples if required. 希望我很清楚,但是如果需要,我可以提供其他示例。

Like in mentioned in the android-united slack, you can accomplish this by using an abstract dao class as base for all your daos. 就像android-united松弛中提到的那样,您可以通过使用抽象的dao类作为所有dao的基础来完成此操作。 just check the file size before performing insert operations. 只需在执行插入操作之前检查文件大小即可。 Using only a DAO: 仅使用DAO:

package com.github.fcopardo.room.base

import android.arch.persistence.room.Delete
import android.arch.persistence.room.Insert
import android.arch.persistence.room.OnConflictStrategy
import android.arch.persistence.room.Update
import android.content.Context
import java.io.File

abstract class LimitedDao<T> {

    private var byteLimit : Long = 0
    private var context : Context? = null
    private var databaseName : String = ""

    public fun setByteLimit(byteLimit : Long){
        this.byteLimit = byteLimit
    }

    public fun setContext(context: Context?){
        this.context = context
    }

    public fun setDatabaseName(databaseName: String){
        this.databaseName = databaseName
    }

    protected fun writingCondition() : Boolean {
        var result = true

        if(byteLimit>0){
            var dbFile : File? = context?.getDatabasePath(databaseName)

            if(dbFile!=null){
                result = dbFile.length() >byteLimit
            }
        }

        return result
    }

    @Insert
    protected abstract fun insert(data: T)

    public fun insertData(data:T){
        if(writingCondition()) insert(data)
    }

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    protected abstract fun persist(data: T)

    public fun persistData(data:T){
        if(writingCondition()) persist(data)
    }

    @Delete
    protected abstract fun delete(data: T)

    @Delete
    protected abstract fun delete(data: List<T>)

    @Update
    protected abstract fun update(data: T)

    @Update
    protected abstract fun update(data: List<T>)
}

A better solution can be achieved by using a base database class too, or you can wrap this logic at viewmodel level; 也可以通过使用基础数据库类来实现更好的解决方案,或者可以将这种逻辑包装在viewmodel级别上。 that would give you safe access to a context, but writing operations done outside the viewmodel would write to the database. 这将使您可以安全地访问上下文,但是在viewmodel外部完成的写操作将写到数据库。 Be mindful of clearing the context reference with setContext(null) when you are done with the dao. 完成dao时,请注意使用setContext(null)清除上下文引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM