objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

Multi process support

Open nickylin opened this issue 6 years ago • 13 comments

we want to use the data in multi process, but i dont know whether this can use ?

nickylin avatar Sep 27 '17 12:09 nickylin

~~You should be with one exception:~~ It is not supportted at the moment. To make it work we have to:

  • reactive queries do not work across processes.
  • ID assignment currently works NOT accross processes - multiple processes would overwrite each other when inserting
  • we have to guard initialization so only one process boots up the DB at a time

greenrobot avatar Sep 27 '17 16:09 greenrobot

If I understood correctly, it's still not possible to listen for changes that originated in different processes. Any idea on when will this be supported?

jpmcosta avatar Nov 14 '17 17:11 jpmcosta

Multi process might be bumpy at the moment. Can you tell us about your use case?

greenrobot avatar Nov 15 '17 08:11 greenrobot

@greenrobot not sure if it's relevant enough to pollute this issue. I have a service in a different process that's responsible to build all the data.

jpmcosta avatar Nov 15 '17 10:11 jpmcosta

It's always relevant to see how features are (intended to be ) used; sometimes it's surprising.

Got it. And why did you decide to give the Service its own process?

greenrobot avatar Nov 15 '17 11:11 greenrobot

It's mainly for better memory management. I might be over-engineering.

jpmcosta avatar Nov 15 '17 11:11 jpmcosta

Sorry, I had to revise my initial comment. Multiple processes are not supported at the moment!

greenrobot avatar Dec 07 '17 11:12 greenrobot

ok got it , thx bro

nickylin avatar Dec 08 '17 06:12 nickylin

Btw, additional read-only processes might even work if they do not rely on reactive queries / data change listeners.

greenrobot avatar Dec 09 '19 14:12 greenrobot

2.7.0 introduced read-only stores that enable multi-process support.

There is an example: https://github.com/objectbox/objectbox-examples/tree/main/android-app-multiprocess

Inter-process data observers are in preparation: #893

greenrobot-team avatar Sep 08 '20 13:09 greenrobot-team

Thanks for working on this subject! I have multiple read and write operations in two separate processes. Currently it it is working quite well for most boxes except one. In this case I write in both processes. For the others it is either one of them writing.

For the one with the issues I have the issue with the error: "Error when updating with existing Id's..." (As a workaround I delete all items and add them again after it....). But also some unexpected loss of items when writing quit fast "couple of seconds" after each other.

Is it planned to support writing from multiple processes in the future?

tomwassink avatar Oct 16 '20 20:10 tomwassink

Is it planned to support writing from multiple processes in the future?

According to our current plans, not in the near future. We advise to use one writer process and use IPC from other processes for writes via the writer process.

greenrobot avatar Oct 17 '20 08:10 greenrobot

in my project, I moved the objectbox.init into service to avoid multiple initialization. and do a code to wait for it to be available:

override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)

        GlobalScope.launch {
            while(true) {
                try {
                    if(ObjectBox.boxStore == null) {
                        Log.d(AdService.TAG, "Waiting for objectbox.")
                        return@launch
                    }

                    return@launch [email protected] {
                        renderApp(savedInstanceState)
                    }
                } catch(e: java.lang.Exception) {
                    // skipped starting issue
                    Log.d(AdService.TAG, "Waiting for objectbox", e)
                }

                delay(1000)
            }
        }
    }

u007 avatar Nov 24 '20 08:11 u007