stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.57k stars 1.54k forks source link

Reactive functionality #1176

Open imseonho opened 1 year ago

imseonho commented 1 year ago

Hello!

I made my application with SQLite, SwiftUI, Combine, MVVM, etc.

Issue

And I needed some feature to refresh view when the database table changes in my app.

I implemented like,

#

And I encountered an error, saying "database is locked (code: 5)", like below.

스크린샷 2022-11-17 오후 7 00 13

The app crashed when I try select action right after insert action.

And I searched about this error, and I got some clue at this. -> Link

The problem was "conflict between two transactions running on the same database connection".

So, to fix this problem, I want to make the select work is done definitely after insert work.

#

Therefore, some reactive functionality can be helpful, so that only when the database table changes, app does select action again to refresh view.

Is there any technique to help reactive view refresh in point of SQLite local DB?

I found some possible method,

But, I have no idea how to actually implement like this. I need more detailed explain...

Build Information

I added SQLite version 0.13.2 with CocoaPods.

'SQLite.swift', '~> 0.13.2'

And I use Xcode version 14.1, macOS Monterey version 12.6.

Related Issue

I checked issue #686,
and I hope to know more information about that.

Any clues will be helpful.

With Love 🫶,
Thank you.

nathanfallet commented 1 year ago

Are you using any asynchronous thing to call your two operations? I think that call the select right after the insert, without any asynchronous thing, should work. If you use two asynchronous methods, the second one can be called before the first one is done.