1010
1111@interface DBManager ()
1212
13- @property (nonatomic , strong ) NSString *documentsDirectory ;
13+ @property (nonatomic , strong ) NSString *appDirectory ;
1414@property (nonatomic , strong ) NSString *databaseFilePath;
1515@property (nonatomic , strong ) NSString *databaseFilename;
1616@property (nonatomic , strong ) NSMutableArray *arrResults;
1717
18- -(void )copyDatabaseIntoDocumentsDirectory ;
18+ -(void )copyDatabaseIntoAppDirectory ;
1919-(void )runQuery : (const char *)query isQueryExecutable : (BOOL )queryExecutable ;
2020
2121@end
@@ -27,30 +27,38 @@ @implementation DBManager
2727-(instancetype )initWithDatabaseFilePath : (NSString *)dbFilePath {
2828 self = [super init ];
2929 if (self) {
30- // Set the documents directory path to the documentsDirectory property.
31- NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
32- self.documentsDirectory = [paths objectAtIndex: 0 ];
30+ // Set the app directory path to the application support files property.
31+ self.appDirectory = [NSSearchPathForDirectoriesInDomains (NSApplicationSupportDirectory, NSUserDomainMask, YES ) firstObject ];
3332
3433 // Keep the database filepath
3534 self.databaseFilePath = dbFilePath;
3635
3736 // Keep the database filename.
3837 self.databaseFilename = [dbFilePath lastPathComponent ];
3938
40- // Copy the database file into the documents directory if necessary.
41- [self copyDatabaseIntoDocumentsDirectory ];
39+ // Copy the database file into the app directory if necessary.
40+ [self copyDatabaseIntoAppDirectory ];
4241 }
4342 return self;
4443}
4544
46- -(void )copyDatabaseIntoDocumentsDirectory {
47- // Check if the database file exists in the documents directory.
48- NSString *destinationPath = [self .documentsDirectory stringByAppendingPathComponent: self .databaseFilename];
45+ // Will be removed in the next major version.
46+ -(void )copyDatabaseIntoAppDirectory {
47+ // Check if the database file exists in the app directory.
48+ NSString *destinationPath = [self .appDirectory stringByAppendingPathComponent: self .databaseFilename];
4949 if (![[NSFileManager defaultManager ] fileExistsAtPath: destinationPath]) {
50- // The database file does not exist in the documents directory, so copy it from the main bundle now.
51- NSString *sourcePath = self.databaseFilePath ;
50+
51+ // Attemp database file migration from the documents directory if exists
52+ NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) firstObject ];
53+ NSString *migrationSourcePath = [documentsDirectory stringByAppendingPathComponent: self .databaseFilename];
5254 NSError *error;
53- [[NSFileManager defaultManager ] copyItemAtPath: sourcePath toPath: destinationPath error: &error];
55+ if ([[NSFileManager defaultManager ] fileExistsAtPath: migrationSourcePath]) {
56+ // Migrate the database file from the documents directory to the app directory
57+ [[NSFileManager defaultManager ] moveItemAtPath: migrationSourcePath toPath: destinationPath error: &error];
58+ } else {
59+ // The database file does not exist in the app directory, so copy it from the main bundle now.
60+ [[NSFileManager defaultManager ] copyItemAtPath: self .databaseFilePath toPath: destinationPath error: &error];
61+ }
5462
5563 // Check if any error occurred during copying and display it.
5664 if (debug) {
@@ -72,7 +80,7 @@ -(void)runQuery:(const char *)query isQueryExecutable:(BOOL)queryExecutable{
7280 sqlite3 *sqlite3Database;
7381
7482 // Set the database file path.
75- NSString *databasePath = [self .documentsDirectory stringByAppendingPathComponent: self .databaseFilename];
83+ NSString *databasePath = [self .appDirectory stringByAppendingPathComponent: self .databaseFilename];
7684
7785 // Initialize the results array.
7886 if (self.arrResults != nil ) {
0 commit comments