0

编辑:现在看起来也不能与模拟器一起使用。

更多信息,似乎如果我安装了一个确实有效的存档版本,然后安装了一个不起作用的版本,就在它上面,一切都很好。但是当我删除存档版本并安装新版本时,那就是一切都停止工作的时候。

我只是在我的 iPhone 上测试我的应用程序,它运行良好,当我将它存档并在我的手机上安装 ipa 时,数据库停止工作。加载数据库时不会发生错误,就像我之前说的,它只是工作。我没有更改任何代码。它仍然可以在模拟器上运行,所以我知道它与数据库的复制有关。这是相关代码:

dbPath=[NSString stringWithFormat:@"%@/Documents/database.sql", NSHomeDirectory()];
// Get the documents directory
NSFileManager *fmngr = [[NSFileManager alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"database.sql" ofType:nil];
NSError *error;
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"didLaunchFirstTime"])
{
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"didLaunchFirstTime"];
    [fmngr removeItemAtPath:[NSString stringWithFormat:@"%@/Documents/database.sql", NSHomeDirectory()] error:&error];

    if(![fmngr copyItemAtPath:filePath toPath:[NSString stringWithFormat:@"%@/Documents/database.sql", NSHomeDirectory()] error:&error])
    {
        // handle the error
        NSLog(@"Error creating the database: %@", [error description]);

    }
}

我的查询看起来像这样,因为我使用 FMDB 来查询数据库。它在用户按下搜索按钮时调用的单独方法中。

FMResultSet *s = [db executeQueryWithFormat:@"SELECT Gurmukhi, ShabadID, FirstLetterStr FROM Shabad WHERE FirstLetterStr LIKE %@", searchString];

我还解压缩了 ipa 以检查数据库是否不是空白的,而事实并非如此。我不知道是怎么回事。

4

1 回答 1

1

您不应该在您的应用程序中硬编码目录路径 - Apple 提供了获取它们的功能:

当你想在你的包中找到文件时,你可以这样得到:

NSString *dbPathOld = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"sql"];

现在,您有了随应用程序提供的 sql 文件的路径。

当你想复制它时,你使用这个代码:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *dbPathNew = [docDir stringByAppendingPathComponent:@"database.sql"];

Other comments:

1) Why not use the NSFileManager *fm = [NSFileManager defaultManager];?

2) With your standard defaults, you are registering them in an initialize method in your app delegate? You are synchronizing them after you update values (so in fact "didLaunchFirstTime" is set the second time)? You might want to add a log message so you can know for sure.

于 2012-08-20T13:27:32.893 回答