0

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?

Hussein Yaqoobi
  • 442
  • 5
  • 20
  • Why does it crash? If it's because of threads, you (obviously) could fix your threading. So tell us... Also, you say it's "expensive" without saying what exactly. E.g. is there some common data for all users? – Markus Junginger Feb 01 '22 at 17:50
  • @Markus Junginger For example, each user has a number of contacts and there are a large number of messages for each contact. I save messages using the message object. Normally to find the messages of each contact, I run a query with three conditions (message recipient ID and message sender ID and message status) on all messages. Now, if I want to separate the messages of each account, the fourth condition will be added and I think this will slow down the query. If not, please tell me. – Hussein Yaqoobi Feb 01 '22 at 18:27

0 Answers0