我在应用程序中使用领域已有一段时间了。现在,我已将Share Extension添加到应用程序中,并且我注意到我需要定义group子目录的领域默认文件路径,以便可以从App和Extension访问相同的数据库。我搜索了几次,并在here的帮助下并在here的帮助下找到了最佳解决方案。
这是我的AppDelegate文件
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// migrateRealm()
// configRealm()
return true
}
private func configRealm() {
let fileURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.com.hjri.khandehland")!
.appendingPathComponent("Library/Caches/default.realm")
let config = Realm.Configuration(fileURL: fileURL)
Realm.Configuration.defaultConfiguration = config
}
private func migrateRealm() {
let fileManager = FileManager.default
let originalDefaultRealmURL = Realm.Configuration.defaultConfiguration.fileURL
//Cache original realm path (documents directory)
let originalDefaultRealmPath = originalDefaultRealmURL?.path
//Generate new realm path based on app group
let realmURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.com.hjri.khandehland")!
.appendingPathComponent("Library/Caches/default.realm")
let realmPath = realmURL.path
if originalDefaultRealmPath != nil {
//Moves the realm to the new location if it hasn't been done previously
if (fileManager.fileExists(atPath: originalDefaultRealmPath!) && !fileManager.fileExists(atPath: realmPath)) {
print("***** FILE EXISTS AND MOVING")
do {
try fileManager.moveItem(atPath: originalDefaultRealmPath!, toPath: realmPath)
} catch {
print("***** REALM FILE PATH MIGRATION ERROR *****")
}
} else {
print ("***** FILE DOES NOT EXIST *****")
}
}
//Set the realm path to the new directory
Realm.Configuration.defaultConfiguration.fileURL = realmURL
}
此外,我的模型类是
class User: Object {
@objc dynamic var username: String = ""
@objc dynamic var email: String = ""
@objc dynamic var password: String = ""
@objc dynamic var accessToken: String = ""
@objc dynamic var refreshToken: String = ""
}
class Message: Object {
@objc dynamic var id: Int = 0
@objc dynamic var content: String = ""
@objc dynamic var submitDate: String = ""
@objc dynamic var submitDateEpoch: Double = 0
@objc dynamic var favoriteCount:Int = 0
@objc dynamic var isFavorite: Bool = false
@objc dynamic var needsSync: Bool = false
@objc dynamic var syncDetails: String = ""
@objc dynamic var isNew: Bool = true
}
首先要测试我的应用程序,然后使用默认的领域文件路径来处理模型,然后取消注释migrateRealm()
或configRealm()
方法之一,但这些方法均无法正常工作。首先,我可以登录(创建用户实例),然后重新启动应用程序,它可以正确读取用户数据,但是消息完全不会出现在应用程序中,这意味着在取消注释时,读取/写入它们时会出现问题。 configRealm
应该只更改领域路径并且不迁移旧数据,该应用仍会向我显示旧消息,但不会加载用户,这意味着我的登录状态在重新启动后消失了,并且另外,如果我更新使领域更改消息模型属性的message属性,它会引发此错误,并且应用程序崩溃:
*由于未捕获的异常“ RLMException”而终止应用程序,原因: '尝试在写事务之外修改对象-调用 首先在RLMRealm实例上执行beginWriteTransaction。 * 初次调用堆栈
此错误表示我正在领域写闭包之外进行更新操作,但是我的代码在更改路径之前运行良好,而且我也确实在写块内编写了更新命令,如您在此处看到的是我的消息更新方法
func update(content: String? = nil, isFavorite: Bool? = nil, needsSync: Bool? = nil, syncDetails: String? = nil, favoriteCount: Int? = nil) {
let realm = try! Realm()
try! realm.write {
self.content = content ?? self.content
self.isFavorite = isFavorite ?? self.isFavorite
self.needsSync = needsSync ?? self.needsSync
self.syncDetails = syncDetails ?? self.syncDetails
self.favoriteCount = favoriteCount ?? self.favoriteCount
}
}
我不知道我是否做错了,或者是领域问题还是XCode问题(我使用Beta版) 此刻很困惑
我使用Realm 3.7.5 XCode 10 Beta
答案 0 :(得分:1)
我通过在每次调用Realm实例时将自定义配置设置为Realm实例来使其工作,使此计算属性变得更容易,并且在我想使用Realm时随时调用它
static var realmInstance: Realm {
let fileURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier)!.appendingPathComponent(databaseFileName)
var config = Realm.Configuration(fileURL: fileURL)
return try! Realm(configuration: config)
}