我在配置中将 Realm 设置 readOnly
为 true
时遇到了一些问题。我正在使用 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/