iOS Realm readOnly 导致空结果

标签 ios database swift realm

我在配置中将 Realm 设置 readOnlytrue 时遇到了一些问题。我正在使用 Swift,但我正在使用普通的 Realm.framework ...也许这会导致错误,但我不这么认为。 (我使用它是因为我不想重构我的整个代码......)

这是我正在谈论的内容的示例。

我尝试过这个:

 if let path = NSBundle.mainBundle().pathForResource("database", ofType: "realm")
        {
            //Alternative:  or move the file from the app bundle into a writable location
            var config = RLMRealmConfiguration()
            config.path = path
            config.readOnly = true

            do {
                try self.realm = RLMRealm(configuration:config)
            } catch {
                    print("ERROR")
                }

我也尝试过这个:

 if let path = NSBundle.mainBundle().pathForResource("database", ofType: "realm")
        {
            //Alternative:  or move the file from the app bundle into a writable location
            var config = RLMRealmConfiguration()
            config.path = path
            config.readOnly = true

            RLMRealmConfiguration.setDefaultConfiguration(config)

            do {
                try self.realm = RLMRealm(configuration: RLMRealmConfiguration.defaultConfiguration())
            } catch {
                    print("ERROR")
                }

他们每次都失败。 (打印:“错误”)我发现问题在于 readOnly 设置为 true。我通过将 readOnly 设置为 false 来使这两个片段正常工作。 您知道如何解决这个问题吗?

我打印了错误,它是:

Error Domain=io.realm Code=2 "Unable to open a realm at path '/Users/UserName/Library/Developer/CoreSimulator/Devices/NR/data/Containers/Bundle/Application/NR/AppName.app/database.realm'" UserInfo={NSFilePath=/Users/UserName/Library/Developer/CoreSimulator/Devices/NR/data/Containers/Bundle/Application/NR/AppName.app/database.realm, NSLocalizedDescription=Unable to open a realm at path '/Users/UserName/Library/Developer/CoreSimulator/Devices/NR/data/Containers/Bundle/Application/NR/AppName.app/database.realm', Error Code=2}

最佳答案

好吧,伙计们,我不知道发生了什么,但现在可以了。

我使用他们的数据库的数据库和他们的数据库的对象制作了一个示例项目,并尝试了我的代码,它起作用了。 (两者)

这让我很困惑,我想也许我的数据库已损坏或对象错误。我用我的数据库替换了示例项目中他们的数据库,并使用了我的对象,令人惊讶的是它有效......

所以我只是删除了我的正常项目中的数据库(就像我之前所做的 1000 次一样)并再次添加它。我只是将它放在我的项目结构的顶部并再次尝试,它现在正在工作......

关于iOS Realm readOnly 导致空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34353936/

相关文章:

mysql - 配方数据库,按成分搜索

mysql - 查询是否可以优化 : Get a records max date then join the max date's values

Swift:通过 ID 获取 UI 对象?

ios - 当使用 modalPresentationStyle = .FormSheet 在 UINavigationController 中推送时,SFSafariViewController 显示不正确

swift - 如何在swift中初始化一个unichar变量?

objective-c - iOS6 和 UITableView 中的离屏 UITextView

ios - 为什么应用程序在 itunes API 中找不到歌曲时会崩溃?

ios - 核心数据保存实体问题

javascript - 以实际大小的 25% 呈现 div 的内容

mysql - 无法在 MySQL 5.5.25 中设置 max_allowed_pa​​cket