从 Realm 数据库中读取数据 (Swift)
Reading Data from Realm Database (Swift)
我是 Realm DataBase 的新手,我需要一种方法来从 realmCloud 读取数据,但要从两个不同的应用程序项目读取数据。我尝试实现这一点的方法是使用查询同步领域。目前,我正在使用单一领域用户在一个应用程序中写入数据,并使用同一个领域用户从另一个应用程序读取数据。问题是从第二个应用程序(用于阅读的应用程序)进行查询不会 return 任何领域对象(我还注意到用户标识符与第一个不同,而且用户权限是没有。
我试过直接从 RealmStudio 设置权限,因为文档没有准确说明如何从代码设置权限
func openRealm() {
do {
realm = try Realm(configuration: SyncUser.current!.configuration())
let queryResults = realm.objects(*className*.self)
let syncSubscription = queryResults.subscribe()
let notificationToken = queryResults.observe() { [weak self] (changes) in
switch (changes) {
case .initial: print(queryResults)
case .error(let error): print(error)
default: print("default")
}
}
for token in queryResults {
print(token.tokenString)
}
syncSubscription.unsubscribe()
notificationToken.invalidate()
} catch {
print(error)
}
}
这个函数在一个app项目中打印数据,但是在同一个用户登录的另一个app项目中使用,并且在项目中引用了相同的classFile,它不会. (注意 SyncUser.current.identifier
也不同
有几个问题。
其中一些调用是异步的,您问题中的代码在同步(检索)数据之前就超出了范围。底线是代码比互联网更快,您需要围绕异步调用设计应用程序流程;在数据可用之前不要尝试使用数据。
例如
let notificationToken = queryResults.observe() { [weak self] (changes) in
//here is where results are fully populated
}
// this code may run before results are populated //
for token in queryResults {
print(token.tokenString)
}
此外,let notificationToken
是一个本地变量,在填充结果之前超出范围。
这些问题非常容易解决。第一个是在等待结果被填充时保持通知令牌活动,第二个是在闭包内处理结果,因为那是它们有效的时候。
var notificationToken: NotificationToken? = nil //a class var
func openRealm() {
do {
let config = SyncUser.current?.configuration()
let realm = try Realm(configuration: config!)
let queryResults = realm.objects(Project.self)
let syncSubscription = queryResults.subscribe(named: "my-projects")
self.notificationToken = queryResults.observe() { changes in
switch changes {
case .initial:
print("notification: initial results are populated")
queryResults.forEach { print([=11=]) }
case .update(_, let deletions, let insertions, let modifications):
print("notification: results, inserted, deleteed or modified")
insertions.forEach { print([=11=]) } //or mods or dels
case .error(let error):
fatalError("\(error)")
}
}
} catch {
print(error)
}
}
deinit {
self.notificationToken?.invalidate()
}
保持该令牌(及其相应代码)处于活动状态的另一个好处是,当有进一步更改时,您的应用程序将收到通知。因此,例如,如果添加了另一个项目,'changes' 部分中的代码将 运行 并显示该更改。
我是 Realm DataBase 的新手,我需要一种方法来从 realmCloud 读取数据,但要从两个不同的应用程序项目读取数据。我尝试实现这一点的方法是使用查询同步领域。目前,我正在使用单一领域用户在一个应用程序中写入数据,并使用同一个领域用户从另一个应用程序读取数据。问题是从第二个应用程序(用于阅读的应用程序)进行查询不会 return 任何领域对象(我还注意到用户标识符与第一个不同,而且用户权限是没有。
我试过直接从 RealmStudio 设置权限,因为文档没有准确说明如何从代码设置权限
func openRealm() {
do {
realm = try Realm(configuration: SyncUser.current!.configuration())
let queryResults = realm.objects(*className*.self)
let syncSubscription = queryResults.subscribe()
let notificationToken = queryResults.observe() { [weak self] (changes) in
switch (changes) {
case .initial: print(queryResults)
case .error(let error): print(error)
default: print("default")
}
}
for token in queryResults {
print(token.tokenString)
}
syncSubscription.unsubscribe()
notificationToken.invalidate()
} catch {
print(error)
}
}
这个函数在一个app项目中打印数据,但是在同一个用户登录的另一个app项目中使用,并且在项目中引用了相同的classFile,它不会. (注意 SyncUser.current.identifier
也不同
有几个问题。
其中一些调用是异步的,您问题中的代码在同步(检索)数据之前就超出了范围。底线是代码比互联网更快,您需要围绕异步调用设计应用程序流程;在数据可用之前不要尝试使用数据。
例如
let notificationToken = queryResults.observe() { [weak self] (changes) in
//here is where results are fully populated
}
// this code may run before results are populated //
for token in queryResults {
print(token.tokenString)
}
此外,let notificationToken
是一个本地变量,在填充结果之前超出范围。
这些问题非常容易解决。第一个是在等待结果被填充时保持通知令牌活动,第二个是在闭包内处理结果,因为那是它们有效的时候。
var notificationToken: NotificationToken? = nil //a class var
func openRealm() {
do {
let config = SyncUser.current?.configuration()
let realm = try Realm(configuration: config!)
let queryResults = realm.objects(Project.self)
let syncSubscription = queryResults.subscribe(named: "my-projects")
self.notificationToken = queryResults.observe() { changes in
switch changes {
case .initial:
print("notification: initial results are populated")
queryResults.forEach { print([=11=]) }
case .update(_, let deletions, let insertions, let modifications):
print("notification: results, inserted, deleteed or modified")
insertions.forEach { print([=11=]) } //or mods or dels
case .error(let error):
fatalError("\(error)")
}
}
} catch {
print(error)
}
}
deinit {
self.notificationToken?.invalidate()
}
保持该令牌(及其相应代码)处于活动状态的另一个好处是,当有进一步更改时,您的应用程序将收到通知。因此,例如,如果添加了另一个项目,'changes' 部分中的代码将 运行 并显示该更改。