I have a lot of troubles porting my sqlite code to the App Store due to the way sqlite executes transactions (using wal or journal files). Relevant part from Apple Documentation is:
Your app needs to be able to open or save multiple related files with the same name and different extensions (for example, to automatically open a subtitle file with the same name as a movie file, or to allow for a SQLite journal file). To gain access to that secondary file, create a class that conforms to the NSFilePresenter protocol. This object should provide the main file’s URL as its primaryPresentedItemURL property, and should provide the secondary file’s URL as its presentedItemURL property. After the user opens the main file, your file presenter object should call the addFilePresenter: class method on the NSFileCoordinator class to register itself.
Apple DTS provides me the following code:
- (void)openSQLiteFileAtURL:(NSURL *)fileURL {
NSFileCoordinator *fc = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fc coordinateReadingItemAtURL:fileURL options:0 error:NULL byAccessor:^(NSURL *newURL) {
sqlite3 *db = NULL;
char *zErrMsg = 0;
[SQLiteRelatedItemPresenter addPresentersForURL:newURL];
int rc = sqlite3_open_v2([[newURL path] fileSystemRepresentation], &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
NSLog(@"open %@ = %d", [newURL path], rc);
rc = sqlite3_exec(db, "CREATE TABLE foo (col1 INTEGER);", callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
NSLog(@"SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
// more sqlite code here
sqlite3_close(db);
}];
return;
}
where SQLiteRelatedItemPresenter is:
@interface SQLiteRelatedItemPresenter : NSObject <NSFilePresenter>
{
NSURL *primaryPresentedItemURL;
NSURL *presentedItemURL;
}
static NSOperationQueue *_presentedItemOperationQueue;
@implementation SQLiteRelatedItemPresenter
+ (void) initialize {
[super initialize];
if (_presentedItemOperationQueue == nil) {
_presentedItemOperationQueue = [[NSOperationQueue alloc] init];
}
}
+ (void)addPresentersForURL:(NSURL *)databaseURL {
SQLiteRelatedItemPresenter *p1, *p2, *p3, *p4;
p1 =[[SQLiteRelatedItemPresenter alloc] initWithFileURL:databaseURL prefix:nil suffix:@"-wal"];
[NSFileCoordinator addFilePresenter:p1];
p2 = [[SQLiteRelatedItemPresenter alloc] initWithFileURL:databaseURL prefix:nil suffix:@"-shm"];
[NSFileCoordinator addFilePresenter:p2];
p3 = [[SQLiteRelatedItemPresenter alloc] initWithFileURL:databaseURL prefix:nil suffix:@"-journal"];
[NSFileCoordinator addFilePresenter:p3];
p4 = [[SQLiteRelatedItemPresenter alloc] initWithFileURL:databaseURL prefix:@"." suffix:@"-conch"];
[NSFileCoordinator addFilePresenter:p4];
// +filePresenters will only return once the asynchronously added file presenters are done being registered
[NSFileCoordinator filePresenters];
}
- initWithFileURL:(NSURL *)fileURL prefix:(NSString *)prefix suffix:(NSString *)suffix {
self = [super init];
if (self) {
primaryPresentedItemURL = fileURL;
NSString *path = [fileURL path];
if (prefix) {
NSString *name = [path lastPathComponent];
NSString *dir = [path stringByDeletingLastPathComponent];
path = [dir stringByAppendingPathComponent:[prefix stringByAppendingString:name]];
}
if (suffix) {
path = [path stringByAppendingString:suffix];
}
presentedItemURL = [NSURL fileURLWithPath:path];
}
return self;
}
- (NSURL *)presentedItemURL {
return presentedItemURL;
}
- (NSOperationQueue *)presentedItemOperationQueue {
return _presentedItemOperationQueue;
}
- (NSURL *)primaryPresentedItemURL {
return primaryPresentedItemURL;
}
This specific example works fine for sqlite operations all executed inside the openSQLiteFileAtURL method. I am having a lot of troubles if I try to divid logic into sub-methods, for example:
- openSQLiteFileAtURL:(NSURL *)databaseURL; // just open the db
- executeSQLStatement:(NSString *)sql; //perform read/write operations into the sqlite db previously opened in the openSQLiteFileAtURL method
- closeSQLite(); //close db method.
Seems like that addPresentersForURL should be called only once (in openSQLiteFileAtURL) but I wasn't able to have a working app due to sandbox privileges errors... any help?