10
10
11
11
@interface DBManager ()
12
12
13
- @property (nonatomic , strong ) NSString *documentsDirectory ;
13
+ @property (nonatomic , strong ) NSString *appDirectory ;
14
14
@property (nonatomic , strong ) NSString *databaseFilePath;
15
15
@property (nonatomic , strong ) NSString *databaseFilename;
16
16
@property (nonatomic , strong ) NSMutableArray *arrResults;
17
17
18
- -(void )copyDatabaseIntoDocumentsDirectory ;
18
+ -(void )copyDatabaseIntoAppDirectory ;
19
19
-(void )runQuery : (const char *)query isQueryExecutable : (BOOL )queryExecutable ;
20
20
21
21
@end
@@ -27,30 +27,38 @@ @implementation DBManager
27
27
-(instancetype )initWithDatabaseFilePath : (NSString *)dbFilePath {
28
28
self = [super init ];
29
29
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 ];
33
32
34
33
// Keep the database filepath
35
34
self.databaseFilePath = dbFilePath;
36
35
37
36
// Keep the database filename.
38
37
self.databaseFilename = [dbFilePath lastPathComponent ];
39
38
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 ];
42
41
}
43
42
return self;
44
43
}
45
44
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];
49
49
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];
52
54
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
+ }
54
62
55
63
// Check if any error occurred during copying and display it.
56
64
if (debug) {
@@ -72,7 +80,7 @@ -(void)runQuery:(const char *)query isQueryExecutable:(BOOL)queryExecutable{
72
80
sqlite3 *sqlite3Database;
73
81
74
82
// Set the database file path.
75
- NSString *databasePath = [self .documentsDirectory stringByAppendingPathComponent: self .databaseFilename];
83
+ NSString *databasePath = [self .appDirectory stringByAppendingPathComponent: self .databaseFilename];
76
84
77
85
// Initialize the results array.
78
86
if (self.arrResults != nil ) {
0 commit comments