Search code examples
iosdbaccess

DBAccess creating custom DBAccessSettings


I am testing DBAccess in order to see if I can use it in a project. I have problems changing the location of the sqlite file, I want it to be in cache directory. Here is part of the code I wrote until now, in AppDelegate.m :

- (DBAccessSettings*)getCustomSettings {
    DBAccessSettings *settings = [[DBAccessSettings alloc] init];
    NSURL *applicationCachesDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
    settings.databaseLocation = [applicationCachesDirectory absoluteString];
    settings.defaultDatabaseName = @"myDefaultDB";
    return settings;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [DBAccess setDelegate:self];
    [DBAccess openDatabaseNamed:@"NSURLProtocolExample"];    
    [NSURLProtocol registerClass:[MyURLProtocol class]];
    return YES;
}

But with the above code I get the following error message:

error >> unable to open database file

Can you shed some light in here?


Solution

  • I'm not familiar with DBAccess, but you have these lines:

    NSURL *applicationCachesDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
    settings.databaseLocation = [applicationCachesDirectory absoluteString];
    

    That will retrieve a URL string (one that starts with file://), not a file path. If you want the path of that folder, you want to use path:

    settings.databaseLocation = [applicationCachesDirectory path];
    

    Frankly, the documentation is ambiguous as to whether its expecting a URL string or a path, but I would assume it expecting the latter.