0

In GRDB I can create read-only database connections with

try dbPool.read { db in

I can create read-write database connections with

try dbPool.write { db in

In each case I can call a function from within the closure using db as an argument. In GRDB read-only database connections can run in parallel in different threads. Write database connections block the database.

I have a pattern I would like to use in some functions:

  • Select a record filtered on some attributes.
  • If a record is found, return it.
  • If no record is found, create a default placeholder record with those attributes.
  • If the db is read-write, save the default placeholder and return it.
  • If the db is read-only, just return the placeholder

Is there a way to determine if the db is read-only? Looking through the reference documentation at http://groue.github.io/GRDB.swift/docs/4.1/Classes/Database.html, I don't see a simple way to determine this aside from error handling.

I could just attempt to save in the function and catch the error if the db is read-only.

4

1 回答 1

0

有没有办法确定数据库是否是只读的?

不,没有任何公开方式可以获取此信息。这是可能的,但功能请求尚未出现。

如果数据库是只读的,我可以尝试保存在函数中并捕获错误。

正确的。

我有一个我想在某些功能中使用的模式 [...]

您描述的模式看起来像是对简单写入的过早优化。您是否注意到实际的性能问题?

于 2019-07-23T06:48:56.127 回答