Преглед изворни кода

1.修改iOS Apex Mobile数据库文件路径。

Pen Li пре 7 година
родитељ
комит
7907d487a1
1 измењених фајлова са 42 додато и 30 уклоњено
  1. 42 30
      Apex Mobile/Apex Mobile/ApexMobileDB.m

+ 42 - 30
Apex Mobile/Apex Mobile/ApexMobileDB.m

@@ -46,7 +46,8 @@
 {
     sqlite3* db = nil;
     
-    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documents = [paths objectAtIndex:0];
     NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
     DebugLog(@"home path %@",documents);
@@ -56,19 +57,19 @@
         sqlite3_close(db);
     }
     
-#ifdef DEBUG_DB
-    NSFileManager *manager = [NSFileManager defaultManager];
-    
-    NSString *copyPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
-    copyPath = [copyPath stringByAppendingPathComponent:DBNAME];
-    [manager removeItemAtPath:copyPath error:nil];
-    BOOL isCopy = [manager copyItemAtPath:database_path toPath:copyPath error:nil];
-    if (isCopy) {
-        NSLog(@"拷贝成功");
-    } else {
-        NSLog(@"拷贝失败");
-    }
-#endif
+//#ifdef DEBUG_DB
+//    NSFileManager *manager = [NSFileManager defaultManager];
+//
+//    NSString *copyPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
+//    copyPath = [copyPath stringByAppendingPathComponent:DBNAME];
+//    [manager removeItemAtPath:copyPath error:nil];
+//    BOOL isCopy = [manager copyItemAtPath:database_path toPath:copyPath error:nil];
+//    if (isCopy) {
+//        NSLog(@"拷贝成功");
+//    } else {
+//        NSLog(@"拷贝失败");
+//    }
+//#endif
     return db;
     
 }
@@ -209,27 +210,38 @@
 + (BOOL) initializeDb {
     DebugLog (@"initializeDB");
     
-    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
-    NSString *documents = [paths objectAtIndex:0];
-    NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
-    
-    
-    // move db file from document to cache ;
-    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-    NSString *documentFolderPath = [searchPaths objectAtIndex: 0];
-    NSString* dbFilePath = [documentFolderPath stringByAppendingPathComponent:DBNAME];
-    if ([[NSFileManager defaultManager] fileExistsAtPath: dbFilePath])
-    {
-        [[NSFileManager defaultManager] moveItemAtPath:dbFilePath toPath:database_path error:nil];
-        
-    }
+//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+//    NSString *documents = [paths objectAtIndex:0];
+//    NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
+//
+//
+//    // move db file from document to cache ;
+//    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+//    NSString *documentFolderPath = [searchPaths objectAtIndex: 0];
+//    NSString* dbFilePath = [documentFolderPath stringByAppendingPathComponent:DBNAME];
+//    if ([[NSFileManager defaultManager] fileExistsAtPath: dbFilePath])
+//    {
+//        [[NSFileManager defaultManager] moveItemAtPath:dbFilePath toPath:database_path error:nil];
+//
+//    }
     // end move;
     
+    // 2019.1.11 move db from cache to document
+    NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
+    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
+    
+    NSString *db_cache = [cacheDir stringByAppendingPathComponent:DBNAME];
+    NSString *db_doc = [docDir stringByAppendingPathComponent:DBNAME];
+    if ([[NSFileManager defaultManager] fileExistsAtPath:db_cache]) {
+        NSError *err;
+        [[NSFileManager defaultManager] moveItemAtPath:db_cache toPath:db_doc error:&err];
+        if (err) {
+            NSLog(@"move database error: %@",err);
+        }
+    }
     
     
     
-
-    
     if (true)//! [[NSFileManager defaultManager] fileExistsAtPath: database_path])
     {
         sqlite3 *db = [self get_db];