Skip to content

When calling the DatabaseManager.getDatabase function, the iOS app crashes. #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
iVanPan opened this issue Mar 7, 2025 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@iVanPan
Copy link

iVanPan commented Mar 7, 2025

Image
var openDatabases = [String: Database]()

public func getDatabase(_ name: String) -> Database? {
     objc_sync_enter(openDatabases)
     defer {
         objc_sync_exit(openDatabases)
      }
   return openDatabases[name]
}

public func open(_ databaseName: String, databaseConfig: [AnyHashable: Any]?) throws {
        do {
            if self.openDatabases[databaseName] != nil {
                self.openDatabases.removeValue(forKey: databaseName)
            }
            let config = self.buildDatabaseConfig(databaseConfig)
            let database = try Database(name: databaseName, config: config)
            self.openDatabases[databaseName] = database
        } catch {
            throw DatabaseError.unableToOpenDatabase(databaseName: databaseName)
        }
    }

    public func delete(_ databaseName: String) throws {
        guard let database = self.getDatabase(databaseName) else {
            throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
        }
        do {
            try database.delete()
            openDatabases.removeValue(forKey: databaseName)
        } catch {
            if let nsError = error as NSError?, nsError.code == 19 {
                // SQLite error code 19 (SQLITE_CONSTRAINT) indicates that the database is locked.
                throw DatabaseError.databaseLocked(databaseName: databaseName)
            } else {
                throw DatabaseError.unableToDeleteDatabase(message: "Error deleting database: \(error.localizedDescription)", databaseName: databaseName)
            }
        }
    }

Is there a data race issue with openDatabases?

@deniswsrosa deniswsrosa added the bug Something isn't working label Mar 10, 2025
@deniswsrosa
Copy link
Contributor

We are trying it out. Will come back to you in a few days.

@azaddhirajkumar
Copy link
Contributor

azaddhirajkumar commented Mar 11, 2025

Hi @iVanPan
Could you please add more context as to in which scenario are you getting this crash? Please add some code snippet of your ionic app or if possible, point to that repository.

@azaddhirajkumar azaddhirajkumar self-assigned this Mar 11, 2025
@gerow-x
Copy link

gerow-x commented Mar 13, 2025

Hi @iVanPan Could you please add more context as to in which scenario are you getting this crash? Please add some code snippet of your ionic app or if possible, point to that repository.

When using an iOS tablet with an Ionic plugin, after receiving a callback, fetching data via GetDocument causes the app to hang/crash consistently after 20 iterations.

@azaddhirajkumar
Copy link
Contributor

When using an iOS tablet with an Ionic plugin, after receiving a callback, fetching data via GetDocument causes the app to hang/crash consistently after 20 iterations.

I tried running over 20 iterations of getDocument, and it works fine in my environment. Could you point me to the specific code, app, or snippet where you're accessing getDocument? That would help me debug the issue further.

@azaddhirajkumar
Copy link
Contributor

azaddhirajkumar commented Mar 25, 2025

I went through the files provided earlier and here are some comments on replication:

It’s good practice to remove change and document listeners when they’re no longer needed, as leaving them active can cause memory leaks, and unexpected behaviour—especially when components unmount or replication is restarted.

Some example cases where you would need to remove listeners:

  • When stopping a replicator (stopSync)
  • When user logs out
  • When unmounting a component tied to the listener
  • Before re-adding new listeners (to avoid duplication)

You can find the documentation here to remove the listeners: https://cbl-ionic.dev/DataSync/remote-sync-gateway

some example replicator tests for reference: https://github.com/Couchbase-Ecosystem/cblite-js-tests/blob/main/cblite-tests/e2e/replicator-test.ts

Example app which shows how to configure database, configure replicator and start replication: https://github.com/couchbase-examples/cap-cbl-travel/blob/main/src/services/database.service.ts

You may want to consider using a Live Query if you expect the result to reflect real-time changes to the database (like inserts, updates, or deletes), especially during or after replication.
Live queries automatically listen to document changes and re-run the query when relevant data changes — this ensures your UI or logic stays in sync without needing to re-run queries manually.

Documentation for live queries: https://cbl-ionic.dev/Queries/live-queries

Example app for live queries: https://github.com/couchbase-examples/cap-cbl-sample/blob/main/src/services/database.service.ts

If the above don't solve the problem, please provide the logs and the workflow of the app.

Example of logs enabling - https://github.com/couchbase-examples/cap-cbl-travel/blob/f53ef16f4195ddf1a7ddd679390445867b1e8127/src/services/database.service.ts#L133

@gerow-x
Copy link

gerow-x commented Apr 25, 2025

When using an iOS tablet with an Ionic plugin, after receiving a callback, fetching data via GetDocument causes the app to hang/crash consistently after 20 iterations.

I tried running over 20 iterations of getDocument, and it works fine in my environment. Could you point me to the specific code, app, or snippet where you're accessing getDocument? That would help me debug the issue further.

In the submitted demo, we only used a single listener to monitor data changes in a Collection. Upon receiving a callback, we called GetDocument to retrieve the document content, with no additional code. After 10-20 invocations, GetDocument stops returning data in the callback.

@azaddhirajkumar
Copy link
Contributor

@gerow-x There have been some improvement in the latest cbl-ionic package. Please try it out and let us know if this issue still persists.

@rajagp
Copy link

rajagp commented May 9, 2025

@gerow-x Did you have chance to test out with latest v1.0 - let us know of issue persists? https://www.npmjs.com/package/cbl-ionic/v/1.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants