Swift + 领域:如何从 collection.find 的范围内更改外部变量
Swift + Realm: How to change an outer variable from inside a collection.find's scope
根据 this 教程,我编写了以下 class:
import RealmSwift
import Darwin
import SwiftUI
let app = App(id: "my-app-id")
class AccessManager: Object {
@objc dynamic var isInTime: Bool = false
func foo2() -> Bool {
return true
}
func foo1() {
app.login(credentials: Credentials.anonymous) { (result) in
DispatchQueue.main.async {
switch result {
case .failure(let error):
print("Login failed: \(error)")
case .success(let user):
print("Login as \(user) succeeded!")
let client = app.currentUser!.mongoClient("mongodb-atlas")
let database = client.database(named: "my-database")
let collection = database.collection(withName: "my-collection")
let identity = "my-identity"
collection.find(filter: ["_partition": AnyBSON(identity)], { (result) in
switch result {
case .failure(let error):
print("Call to MongoDB failed: \(error.localizedDescription)")
case .success(let documents):
self.bar = self.foo2()
print(self.bar) // prints true
}
})
print(self.bar) // prints false
}
}
}
}
}
当我在 collection.find
的范围内更改 self.bar
的值时(使用 self.foo2
函数),它的值不会在该范围之外更改 - 即第一个 print(self.bar)
- true
正在打印,但在第二个 - false
被打印。
如何更改 self.bar
的值,以便更改在 collection.find
的范围之外也能生效?
正如@Jay 评论的那样:
closures are asynchronous and the code following the closure will
(may) execute before the code in the closure. So that code will print
false before the value is set to true. Code is faster than the
internet so data is only valid in the closure.
这就是为什么在我的例子中,闭包外的 print(self.bar)
在 collection.find
闭包之前执行。因此它的结果是 false
而不是 true
.
根据 this 教程,我编写了以下 class:
import RealmSwift
import Darwin
import SwiftUI
let app = App(id: "my-app-id")
class AccessManager: Object {
@objc dynamic var isInTime: Bool = false
func foo2() -> Bool {
return true
}
func foo1() {
app.login(credentials: Credentials.anonymous) { (result) in
DispatchQueue.main.async {
switch result {
case .failure(let error):
print("Login failed: \(error)")
case .success(let user):
print("Login as \(user) succeeded!")
let client = app.currentUser!.mongoClient("mongodb-atlas")
let database = client.database(named: "my-database")
let collection = database.collection(withName: "my-collection")
let identity = "my-identity"
collection.find(filter: ["_partition": AnyBSON(identity)], { (result) in
switch result {
case .failure(let error):
print("Call to MongoDB failed: \(error.localizedDescription)")
case .success(let documents):
self.bar = self.foo2()
print(self.bar) // prints true
}
})
print(self.bar) // prints false
}
}
}
}
}
当我在 collection.find
的范围内更改 self.bar
的值时(使用 self.foo2
函数),它的值不会在该范围之外更改 - 即第一个 print(self.bar)
- true
正在打印,但在第二个 - false
被打印。
如何更改 self.bar
的值,以便更改在 collection.find
的范围之外也能生效?
正如@Jay 评论的那样:
closures are asynchronous and the code following the closure will (may) execute before the code in the closure. So that code will print false before the value is set to true. Code is faster than the internet so data is only valid in the closure.
这就是为什么在我的例子中,闭包外的 print(self.bar)
在 collection.find
闭包之前执行。因此它的结果是 false
而不是 true
.