Search code examples
kotlintornadofxkotlin-exposed

Can I get a TableView column to use a Kotlin Exposed transaction when referencing a property?


When using TornadoFX, TableView columns are established like this:

tableview(list<ObjectType>) {
   column("ColumnName", ObjectType::property)
   ...
}

This is normally fine, but in my case I'm using a Kotlin Exposed entity that's using a reference to another entity. When that happens, if you want to use that reference, you have to surround it in a transaction.

Example:

val company = transaction { employeeObject.companyObject }

If you don't wrap a call like that in a transaction, an error gets thrown. There doesn't seem to be an obvious way to override how a column accesses a property, so I'd like to know if it exists.

Now, I've already tried to wrap my entity in another class that would do all the necessary transactions up front but when the amount of entities that need to be mapped gets in the thousands, it causes my program to basically go into a stand still. If need be, I can go back to how it used to be, which was to not have a reference, but just the plain old ID number to the other entity. Then the cellformat of the column would try to match the company to all companies in a list that was grabbed earlier. I don't really like that solution though, it seems uglier and less elegant, but it's a lot faster than mapping entities. There's also the chance that what I'm trying to achieve might also cause its own slow-down. I'd just like to know if this is possible so I could at least see how fast it is.


Solution

  • Thanks, Edvin, for reminding me that columns work on the UI thread and that it shouldn't be doing the heavy lifting! I tried a few other things with mapping, hoping that the choke point was the amount of transactions I was doing, but it didn't help with speed. So I think having my View retrieve a list of all companies up front, then having my columns find the companies in that list is the way to go. Not as pretty, but no slow down!

    But to officially answer my own question: It doesn't matter, don't try it in the UI thread. It's bad practice and will kill performance.