storesafe / cordova-sqlite-storage-help

Help forum for Cordova sqlite plugin projects
2 stars 1 forks source link

Using database object in native code, with Swift #65

Open jigyasu11 opened 5 years ago

jigyasu11 commented 5 years ago

Hi there,

We are using sqlite-plugin to handle db operations in ionic app side. We have our own plugin that needs to interact with database as well. Its has been observed that sometimes we get 'db locked' errors. On root causing the issue, the obvious suspect was indeed the culprit! In our plugin, for every execution we open the db and when plugin exits close the db.

    init() {
        self.db = self.open()
    }

    deinit {
        if sqlite3_close(db) != SQLITE_OK {
            print("error while closing database")
        } else{
            print("successfully closed database")
        }

    }

Since our plugin gets executed multiple time in quick succession, results in multiple db objects live on the fly. Hence I was wondering, if we can somehow re-use the same database object that sqlite-plugin uses. This will be a robust solution as well. Is it possible? Thanks so much, Richeek

brodycj commented 5 years ago

Thanks for reporting this issue. It is clearly an issue with using the sqlite plugin together with a custom plugin written in Swift. I have seen this kind of an issue multiple times now.

The recommended solution is to completely remove any kind of existing sqlite plugin and use https://github.com/xpbrew/cordova-sqlite-express-build-support instead. It uses the builtin SQLite libraries on iOS, Android, and macOS (does not support Windows).

In general I would not encourage using the same database object that the sqlite plugin uses. I think it would be much safer and cleaner for the custom plugin to open and use its own sqlite database object. I think SQLite was designed to work this way.