2015-04-02 86 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