This message was deleted.
# coroutines
s
This message was deleted.
c
If you create a thread pool with a single thread, then you are guaranteed that coroutines will resume in the same thread (https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/new-single-thread-context.html), not sure if that's enough for you?
e
Maybe, but what if I want a dynamically growing (and shrinking?) thread pool with configurable maximum size? I think that
<http://Dispatchers.IO|Dispatchers.IO>
does that with a default maximum size of 64 threads (https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-i-o.html)
With more than 1 thread only, I can have a bit more concurrency: while one operation is suspended (blocking a thread), another operation can still happen (on another thread). That isn't possible with just one thread, I think, if the library (Realm) does blocking operations.
u
You should avoid blocking coroutines. If your operation is blocking, you should wrap just that operation in
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... }
. This will allow your single thread pool to continue working while the operation is suspended on the
IO
dispatcher.
e
I see your point, and its similar to the point that I was trying to make, but I think I made a fundamental mistake. So I'm deleting my question. Thanks!
c
There are no stupid questions. I think it should be possible to do what you want, but I honestly don't know how. Hopefully someone more knowledgeable comes in
e
A single threaded context is an idea, but the issue is that Realm requires access to all its live instances on the same thread. So blocking IO operations must happen on the same thread. I cannot switch to the IO dispatcher for that.
Alternatively I could try to obtain an instance of the thread-confined objects (often multiple) after switching to the IO dispatcher. Then I can just do all the blocking stuff I want. Trouble is: how? I would be passing around a lot of lambdas instead of actual instances. That's likely too complex.
c
Not really adding anything but it seems really weird to me that a lib would do blocking IO with objects confined to a single thread. Blocking IO is usually the number 1 thing people want to parallelize or do in another thread
e
Welcome to Realm for Android!
It has synchronous (blocking) APIs and asynchronous APIs. The latter are also thread confined in a different way: they must run on a thread with a
Looper
. By default that's the main thread only, which invites doing database stuff from UI classes. Yes, I'm talking years back when these kinds of things happened. Now it's 2021 and that's the legacy I'm trying to modernize.
u
If your library really requires all blocking calls to be executed on the same thread, couldn't you then create a dedicated, single-threaded dispatcher and just use
withContext(yourSingleThreadedDispatcher) { .. }
instead of
<http://Dispatchers.IO|Dispatchers.IO>
? It sounds strange, though
e
Yes, that's possible, but very much impractical, because all objects produced by Realm are also bound to that thread (and to the open Realm instance). 2 options (at least): • Don't pass around these objects, because you also need to pass the thread with them and keep the Realm open. • Obtain a Realm instance on every thread where you might need to CRUD any data. That breaks unidirectional data flow. And has serious concurrency issues
The first option basically means that I need a global/singleton single thread context to which I have to delegate everything. Even reading a simple property needs to happen on the correct thread!
Alternatively, Realm offers to copy objects into memory. That can easily lead to memory issues because copies often need to be deeper than you want. It's also the fault of my data models, but that's also the legacy that I deal with
Yet another option is to freeze a Realm to make it thread safe. It becomes read only and it is stored in disk. Trouble is: the original hot Realm must stay open.
c
Honestly I think your best bet is to have a single thread context and delegate to it. It won't require much work from your end, it's not perfect but it's still much better than using the main thread, and you can have coroutines properly everywhere else in the project. Create a wrapper class for your realm instance and have that context in it, and force the rest of the code to access it from there, and it should be fine
e
Trouble is: even if I get an entity instance from a Realm db, that too must be accessed from that same single thread. So even if I want to read
person.name
I must know the thread I must do that on. 🤦‍♂️ It's like the opposite of the 'shared mutable state' problem. It's unshared mutable state. As in: unshareable. And mutable from multiple threads. That mutability is what I can confine to one thread, but that doesn't give me the freedom to read from any thread. I just cannot pass around instances without also knowing the single thread to CRUD on. Wrapping every CRUD operation in a singleton single threaded context might be possible, but that sounds like horrible design.
c
I'm not saying it's good design, just that it's better than having to do everything in the main thread 😅 Depending on how complex your data is, it could be possible to wrap CRUD in the single thread context, and copy all objects that come from there so other threads can access them?
This way, you'd have a ‘realm wrapper' that does your operations in a single thread, but everything else in your app would run in whatever you want (and you would never block the main thread)
e
We tried that approach, but copies needed to be deeper than was possible with available memory. The data models are too complex for that. If we'd want that approach, we'd need to extract only the data needed into UI specific models, but that's too much work at this time. In any case it's a lot of work. Decoupling UI models from Realm models and behaviour would be an important step forward. Any approach likely needs that, ultimately.