Arno Gramatke
2017-10-24 15:14:37 UTC
Hi all,
in an iOS app we have been storing multiple user „documents“ (hierarchical data mostly, but some larger blobs (~2MB) as well) in a single data base file, that was stored in the app’s "Application Support“ folder. That has worked without problems so far.
Since iOS 11 we would like to give the users the ability to access their documents through iOS’s new Files.app and from within other apps. Therefore we would like to move from the „one db files holds all documents“ approach to an approach where each db file only holds one document. So far so good.
These single files should be placed in the app’s „Documents“ folder, making them accessible from other apps or the Files.app when allowed by the user. With the single db file from above it was only ever our app to access this file and we wouldn’t have to worry about other processes trying to copy, move, delete the file. Now with the files stored in the „Documents“ folder this has changed and we can’t tell when another process tries to access the db files. For this Apple recommends using file coordination where each read or write is coordinated through an NSFileCoordinator.
Has anybody have experience how to approach this?
My first naive approach was to figure out, which commands will result in a read and which will result in a write to the db file. Then coordinate a read or write depending on the statement. For example, an INSERT statement will result in a write to the file (I guess). A SELECT might result in a read (I am not sure, there might some data already being cached?). It seems that processing each statement inside a coordinated read or write would lead to a lot of overhead and I guess this is not what the file coordination was made for. It rather looks like this was meant for infrequent writes to file or even file operations where the whole would be read or written in one go.
Another idea was to copy the db from the file into an in-memory database using file coordination, make all modifications in-memory and then copy the in-memory back to the file system.
Can anyone point me in the right direction? Maybe with a hint how Apple is doing this when working with Core Data and the SQLite storage?
Thanks
Arno
in an iOS app we have been storing multiple user „documents“ (hierarchical data mostly, but some larger blobs (~2MB) as well) in a single data base file, that was stored in the app’s "Application Support“ folder. That has worked without problems so far.
Since iOS 11 we would like to give the users the ability to access their documents through iOS’s new Files.app and from within other apps. Therefore we would like to move from the „one db files holds all documents“ approach to an approach where each db file only holds one document. So far so good.
These single files should be placed in the app’s „Documents“ folder, making them accessible from other apps or the Files.app when allowed by the user. With the single db file from above it was only ever our app to access this file and we wouldn’t have to worry about other processes trying to copy, move, delete the file. Now with the files stored in the „Documents“ folder this has changed and we can’t tell when another process tries to access the db files. For this Apple recommends using file coordination where each read or write is coordinated through an NSFileCoordinator.
Has anybody have experience how to approach this?
My first naive approach was to figure out, which commands will result in a read and which will result in a write to the db file. Then coordinate a read or write depending on the statement. For example, an INSERT statement will result in a write to the file (I guess). A SELECT might result in a read (I am not sure, there might some data already being cached?). It seems that processing each statement inside a coordinated read or write would lead to a lot of overhead and I guess this is not what the file coordination was made for. It rather looks like this was meant for infrequent writes to file or even file operations where the whole would be read or written in one go.
Another idea was to copy the db from the file into an in-memory database using file coordination, make all modifications in-memory and then copy the in-memory back to the file system.
Can anyone point me in the right direction? Maybe with a hint how Apple is doing this when working with Core Data and the SQLite storage?
Thanks
Arno