Sandbox and SQLite

OS X 10.9.5

Xcode Version 6.2 (6C131e)


My app uses SQLite databases as the user documents, and I am having problems using an SQLite3 database from a Sandboxed App, specifically with the temporary files that SQLite creates while modifying a database. If I put the database in the App’s container, it works, but the App Sandbox Design Guide says “the container is not for user documents.”


When Sandbox is activated, attempts to modify the database give a result code of…

“(14) SQLITE_CANTOPEN

The SQLITE_CANTOPEN result code indicates that SQLite was unable to open a file. The file in question might be a primary database file or on of several temporary disk files.”

This presumably is because of the temporary files that SQLite creates while updating the database.


Posts on internet fora suggest modifying the sqlite temporary files directory variable, SQLITE_EXTERN char *sqlite3_temp_directory. However, the sqlite documentation state “Applications are strongly discouraged from using this global variable.”

I got a reference to the Cache folder within the App’s container, and set the variable to that path. To find the folder, I used

NSURL *tempFolder = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];

This did not help. I got the same return code when attempting to modify the database.


From the App Sandbox Design Guide discussion of using NSFilePresenter to handle files related to the document file…

“Note: In the case of a SQLite journal file, beginning in 10.8.2, journal files, write-ahead logging files, and shared memory files are automatically added to the related items list if you open a SQLite database, so this step is unnecessary.”

Does this depend on the file extension of the sql database? SQLite does not care about it, but is the Sandbox looking for a particular file extension to recognise the SQLite database?


Or do I need to switch to Core Data?


Thanks

Replies

What sandbox violation are you getting? Or are you just getting the SQLite error?


You say these are user documents, so are you using NSOpenPanel and NSSavePanel to access them?


--gc

The only indication of a problem is the SQLite error.


The files are being accessed using NSOpenPanel and NSSavePanel, as well as Recent Items menu.