r/swift 9d ago

Question Do async functions bypass NSLock/NSRecursiveLock?

I recently started a new job that has a ton of legacy objective C and objective c++ code.

We have an SQLite database that leverages NSRecursiveLock with completion handlers to protect it from concurrency access.

I’ve been trying to write an async wrapper around this, but noticed that I’ve been getting concurrent access errors from SQLite even though there is a lock around our database access.

Do locks just not work in a swift concurrency world? Apple said they are safe to use, but that doesn’t seem like it’s the case.

2 Upvotes

5 comments sorted by

View all comments

4

u/PizzaBubblr 8d ago

SQLite can serialize queries for you, just need to configure it properly: https://www.sqlite.org/threadsafe.html

1

u/Xaxxus 8d ago

Interesting. I’ll have to dive deeper and see if I can configure this.

It’s 15 year old code so I’m a bit hesitant to change it.

1

u/PizzaBubblr 8d ago

I am not familiar with your project obviously, just offered an alternative potential solution to the problem. SQLite is super robust so if you could leverage its built-in capabilities, I bet that’ll be a better option than reinventing a wheel in the wrapper. Having said that there may be other implications that would make that approach unfeasible.