0

我们react-naive-sqlite-2在我们的应用程序中使用RxDB并开始偶尔出现此错误。它似乎发生在我们删除数据库之后。我很惊讶地看到这是一个WebSQL错误,因为我们使用的是 react-native 并且WebSQL已被弃用。我没有很好的方法来调试它,但我的直觉是我们有一些代码仍然试图访问现在已经死掉的数据库。

这是我们用来设置数据库的代码:

import SQLiteAdapterFactory from 'pouchdb-adapter-react-native-sqlite'
import SQLite from 'react-native-sqlite-2'
import { addRxPlugin, createRxDatabase } from 'rxdb'
import { RxDBReplicationGraphQLPlugin } from 'rxdb/plugins/replication-graphql'

import type { DatabaseType } from '../generated'

/**
 * SQLITE SETUP
 */
const SQLiteAdapter = SQLiteAdapterFactory(SQLite)
addRxPlugin(SQLiteAdapter)
addRxPlugin(require('pouchdb-adapter-http'))

/**
 * Other plugins
 */
addRxPlugin(RxDBReplicationGraphQLPlugin)

export const getRxDB = async () => {
  return await createRxDatabase<DatabaseType>({
    name: 'gatherdatabase',
    adapter: 'react-native-sqlite', // the name of your adapter
    multiInstance: false,
  })

问题发生在我们注销并尝试重新登录后。当我们注销时,我们调用removeRxDatabase. 有没有人遇到过这种问题或知道调试方法?

4

1 回答 1

0

对于后代,问题是我们在状态管理库 (Zusand) 中引用了数据库,该数据库被保留在过去的注销中。当我们再次尝试登录时,我们的getOrCreateDatabase函数并没有创建一个新的,但它是无效的,因为我们已经database.remove()在 rxdb 中运行。我们最终只是清除了 Zusand 数据库并database.remove()在一个地方调用。

export const useRxDB = create<UseRxDBType>((set, get) => ({
  database: undefined,
  /**
   * we set the database to ready in the LocalDocument store once local docs are loaded into the store
   */
  databaseIsReady: false,
  getOrCreateDatabase: async () => {
    let database = get().database

    if (!database) {
      database = await createRxDatabase()

      if (!Rx.isRxDatabase(database)) throw new Error(`database isnt a valid RxDB database.`)

      set({ database })
    }

    return database
  },
  resetDatabase: async () => {
    const database = get().database

    if (database) {
      await database.remove()
      set({ database: undefined })
    }
  },
}))
于 2021-08-06T15:52:43.873 回答