I have an application where a user logs in to their account and stores data according to their account. The user then logs out of their account and logs in with another account and stores other data according to the new account. Now if the user logs out of the new account and logs in to the previous account, the previous data must be displayed to him. In fact, the data of each account should not be deleted, and displayed only for the same account.
One solution that came to my mind was to store a field for each object called the account ID and query it to display the data for each account. But given that every user has a lot of data, I think this is a expensive solution.
Another way I tried was to name the database of each account with the account ID. Like this:
object ObjectBox {
lateinit var store: BoxStore
private set
fun init(context: Context, account: String) {
store = MyObjectBox.builder()
.androidContext(context.applicationContext)
.name(account)
.build()
}
}
In this way, I have to initialize the BoxStore
every time the user logs in:
if (!BoxStore.isDatabaseOpen(context, account))
ObjectBox.init(context, account)
And every time the user logs out of her account, I close the BoxStore
:
ObjectBox.store.closeQuietly()
But the ObjectBox says:
Closes the BoxStore and frees associated resources. This method is useful for unit tests; most real applications should open a BoxStore once and keep it open until the app dies. WARNING: This is a somewhat delicate thing to do if you have threads running that may potentially still use the BoxStore. This results in undefined behavior, including the possibility of crashing.
And when I use it this way, sometimes the app crashes.
Is there another way to separate each user's data?