IOS - access to a single SQL database from JavaScript and FMDB

We have a problem that only appears on an iOS device, but seems to work on a simulator. Here's the problem ...

  • Our iOS app is a hybrid (Cordova) with some species that are completely native, while others are completely web.
  • we would like to use the same sqlite db from both codebases.
  • On the Internet, we use the WebSQL api (not the cordova plugin), on the Native iOS side we use FMDB.
  • The database is initially created from javascript and placed in the application library directory
    • 4.x Dir <AppDir>/Library/WebKit/Databases/file__0/0000000000000001.db
    • 5.x Dir <AppDir>/Library/Caches/file__0/0000000000000001.db
  • Whenever the sqlite database accesses FMDB, the JS code can no longer trigger transactions with the database it created.

While there are other similar SO-issues, I haven’t seen one to which the database was supposed to access both via the Internet and from the native. . Based on my research, it seems to be a sandbox issue that only appears on the device. Here is the code we use to open the database.

 NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDir = [libraryPaths objectAtIndex:0]; NSString *databasePath = [libraryDir stringByAppendingPathComponent:@"WebKit/Databases/file__0/"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:databasePath]) { databasePath = [libraryDir stringByAppendingPathComponent:@"Caches/file__0/"]; } NSString *databaseFile = [databasePath stringByAppendingPathComponent:@"0000000000000001.db"]; if (!static_fmdb) { static_fmdb = [FMDatabase databaseWithPath:databaseFile]; NSAssert(static_fmdb, @"Unable to open create FMDatabase"); } if (![static_fmdb open]) { NSLog(@"Error in %@: Failed to connect to database!\n", NSStringFromSelector(_cmd)); } 

Please note that after running this code, subsequent database calls from the JS side result in the following error: "cannot open transaction in database"

+6
source share
1 answer

We were able to get the desired result here, discarding the migration in our sqlite database until the first migration, effectively providing us with a clean database. We also did not delete cache files created by the web view. This is not the best solution in the world, but it seems to work for our use. If there are any other answers, I would like to know more about them.

0
source

Source: https://habr.com/ru/post/923503/


All Articles