iphone - 在文档目录中复制数据库有问题
<p><p>我有一个应用程序,我需要将我的数据库文件复制到文档目录。我知道该怎么做,而且效果很好。这是它的代码</p>
<pre><code>// Get Required Path
NSFileManager *fileManager = ;
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
NSString *writableDBPath = ;
// Check if DB file already Exist. If YES than return.
success = ;
if (success) return success;
// If DB file is not exist try to write file. This will execute first time only.
NSString *defaultDBPath = [[ resourcePath] stringByAppendingPathComponent:DataBaseName];
success = ;
// Add Skip Backup attribute here.
</code></pre>
<p>这工作正常,但由于某些原因有时会产生错误。现在的问题是,当我尝试跟踪此问题时,它不会发生。</p>
<p>我不知道为什么,但 <code>success = ;</code> 返回 NO。比它尝试写入新文件但写入新文件也会失败。 <code>error</code> 很难追踪,因为它在我的测试中没有重现。</p>
<p>任何人都可以告诉我由于发生这种情况而可能出现的错误并提供解决方案。</p>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试下面的代码</p>
<pre><code>BOOL copySucceeded = NO;
// Get our document path.
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = ;
// Get the full path to our file.
NSString *filePath = ;
// Get a file manager
NSFileManager *fileManager = ;
// Does the database already exist? (If not, copy it from our bundle)
if(!) {
// Get the bundle location
NSString *bundleDBPath = [ pathForResource:fileName ofType:nil];
// Copy the DB to our document directory.
copySucceeded = [fileManager copyItemAtPath:bundleDBPath
toPath:filePath
error:nil];
if(!copySucceeded) {
//not Copy
}
else {
// Success
}
}
else {
// Nothing
}
</code></pre></p>
<p style="font-size: 20px;">关于iphone - 在文档目录中复制数据库有问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16953892/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16953892/
</a>
</p>
页:
[1]