Has anyone had a crash on iOS of type terminateWit...
# kotlin-native
l
Has anyone had a crash on iOS of type terminateWithIllegalSharingException? I’m trying to insert a simple object from main thread to sqldelight using native-mt.
k
Maybe the line before it. Any idea what's calling`toKotlin` ? What's the lambda in SqlMilestoneStore look like?
l
Interesting, I don’t think I have this
toKotlin
on the codebase. Here’s the insert function of SqlMilestoneStore:
Copy code
suspend fun insert(milestones: List<Milestone>): Unit = withContext(context) {
    val time = measureTime {
      milestoneQueries.transaction {
        milestones.forEach { milestone ->
          milestoneQueries.insert(
            achievementId = milestone.achievementId,
            progress = milestone.progress,
            achievedAt = milestone.achievedAt,
            celebratedAt = milestone.celebratedAt,
          )
        }
      }
    }
    // TODO integrate Kermit
    //Timber.d("[Profiler] Inserted ${milestones.size} milestones in ${time}ms.")
  }
The context is
Dispatchers.Default
, On Swift we’re calling it like:
Copy code
func saveMilestones(_ milestones: [CelebratedMilestone]) {
        milestoneStore.insert(milestones: milestones.map { Milestone(achievementId: $0.achievementId,
                                                                     progress: 100,
                                                                     achievedAt: nil,
                                                                     celebratedAt: $0.celebratedAt) }) { _, error in }
    }
that’s on the main thread
k
"// TODO integrate Kermit" Is that really in there 🙂
toKotlin
is maybe coming from K/N and integrating iOS objects. My (wild) guess is somehow the state in the
milestones: List<Milestone>
isn't transitively frozen. Going through
withContext(context)
should freeze that lambda and all state captured within. If Milestone is an iOS extension of some type, maybe that's hiding the freeze call? That's a weird one, though. I'd have to play with it.
l
haha it really is (thanks for writing it btw!) 🙂
Milestone
(and
CelebratedMilestone
are actually sqldelight’s model from the milestone table, from a different projections. It creates the these entities like so:
Copy code
class Foo: ViewController {
    private var listOfASqlDelightModel = //

    func markAsCelebrated(items: [ASqlDelightModel]) {
        let celebratedAt = Date()
        milestoneDataSource.saveMilestones(items.map({ item in
            CelebratedMilestone(item, celebratedAt: celebratedAt)
        }))
    }

    func save() {
        markAsCeleberated(items: listOfASqlDelightModel)
    }
}
i guess it freezes the lambda inside
markAsCelebrated(items:)
. Hopefully not too confusing. But trying to funnel down it is sketchy that
var
property declared and it seems to be the issue. Does it make sense? I’m looking at this for so long that I’m biased and mentally tired.
It has to be something else, because even if I replace passing this var property with a list instantiated inside the save() body, it still crashes 😕