2015-04-02 90 views
-1
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentDir = [documentPaths objectAtIndex:0]; 

self.databasePath = [documentDir stringByAppendingPathComponent:kAppDatabase]; 


-(void) copyDataBaseIfNotExist 

{ 
    BOOL success; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

success = [fileManager fileExistsAtPath:self.databasePath]; 

if(success) return; 

//This block will run for the first time only 

NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kAppDatabase]; 

// Make a copy of the file in the Documents folder 
[fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil]; 
db = [FMDatabase databaseWithPath:self.databasePath]; 

[self insertDummyData]; 

NSLog(@"%@ Database path %s ",self.databasePath,__PRETTY_FUNCTION__); 

}文件路徑不斷變化

上面的代碼拷貝文件到文件夾中。現在是第一次保存。該文件路徑是:

811E4F6C-B282-402A-859B-40FA6DBD2B3C/data/Containers/Data/Application/E6F63F62-30F6-4580-BF4D-C16DE2B33292/Documents/AppDatabase.db 

第二次從訪問:

811E4F6C-B282-402A-859B-40FA6DBD2B3C/data/Containers/Data/Application/48771A22-0616-4162-9DF9-8C5CC4AF6D39/Documents/AppDatabase.db 

爲什麼會改變每一次,而不是它應該是一樣的嗎?

+0

你爲什麼在意?你應該總是使用'NSSearchPathForDirectoriesInDomains'。 – 2015-04-03 00:59:59

+0

我想存儲數據庫中返回的路徑,以便稍後訪問它。但文檔路徑不斷變化。什麼可能是解決方案? – 2015-04-03 01:51:19

+0

不要存儲完整路徑,只存儲您追加的部分。 – 2015-04-03 01:54:23

回答

1

如果您在iOS8中運行您的應用程序,它將保持更改路徑。 結帳在這裏:http://pinkstone.co.uk/where-is-the-documents-directory-for-the-ios-8-simulator/

+0

是爲ios8運行。什麼可能是解決方案? – 2015-04-02 23:06:58

+0

檢查給定的鏈接,你可能會得到答案。有了iOS8和Yosemite,出於安全原因Apple推出了這個技術:) – sample 2015-04-02 23:09:10

+0

它僅僅是用於模擬器還是用於設備呢? – 2015-04-02 23:09:32