Completion/Threading 订单协助
Completion/Threading order assistance
我弄乱了我的完成处理程序的顺序,可能还有我的线程。我使用打印语句和计时器来检查我的工作,这是我发现的:
- 延时为 0.00076 秒,这显然不是预期的行为。
- 第 4 步立即显示,这又不是预期的行为。
- 显示第 1 步
- 第 2 步的显示时间为 2.92 秒,没问题。
唯一需要解决的问题是将第 4 步放在最后,因为这意味着第 1 步和第 2 步已完成。
这个函数在classA:
static func loadDataAndSignIn(completion: @escaping () -> () ) {
let start = Date()
Auth.auth().signInAnonymously { (result, error) in
if result != nil {
print("Step 1 -> Signed In. UserId is: \(result?.user.uid)")
Variables.getDataFromFirestore {
let end = Date()
print("Step 2 - > Data Fetched")
print("Data Fetched in \(end.timeIntervalSince(start)) seconds")
}
} else {
print(error?.localizedDescription ?? print("Sign in Error"))
}
}
DispatchQueue.main.async {
completion()
print("Step 4 -> Signed In and Data Loaded")
}
}
这个函数也在classA:
static func getDataFromFirestore(completion: (() -> Void)? = nil) {
let db = Firestore.firestore()
db.collection("Offences").getDocuments { (snapshot, error) in
if let error = error {
print("Error getting documents: \(error.localizedDescription)")
} else {
Variables.offencesArray.removeAll()
for offence in snapshot!.documents {
let offenceName = offence.get("name") as! String
let offencePoints = offence.get("points") as! Int
let offenceCost = offence.get("cost") as! Int
let offenceSection = offence.get("section") as! String
let offenceCategory = offence.get("category") as! String
let offenceObject = Offence(section: offenceSection, name: offenceName, cost: offenceCost, points: offencePoints, category: offenceCategory)
Variables.offencesArray.append(offenceObject)
}
}
DispatchQueue.main.async {
completion?()
}
}
}
这是在 class B:
的 viewDidLoad 中
let start = Date()
Variables.loadDataAndSignIn {
self.removeLoadingScreen()
let end = Date()
print("Time lapsed: \(end.timeIntervalSince(start))")
}
需要帮助才能让我的 threading/completion 块恢复原状。
将对 completion
的调用放入 getDataFromFirestore
的完成处理程序中,它位于 signInAnonymously
的完成处理程序中:
static func loadDataAndSignIn(completion: @escaping () -> () ) {
Auth.auth().signInAnonymously { (result, error) in
if result != nil {
print("Step 1 -> Signed In. UserId is: \(result?.user.uid)")
Variables.getDataFromFirestore {
let end = Date()
print("Step 2 - > Data Fetched")
print("Data Fetched in \(end.timeIntervalSince(start)) seconds")
DispatchQueue.main.async {
completion()
print("Step 4 -> Signed In and Data Loaded")
}
}
} else {
print(error?.localizedDescription ?? "Sign in Error")
}
}
}
注意:如果对 signInAnonymously
的调用中的 result
为零,您可能还想调用完成,例如:
[...]
} else {
print(error?.localizedDescription ?? "Sign in Error")
DispatchQueue.main.async {
completion()
}
}
我弄乱了我的完成处理程序的顺序,可能还有我的线程。我使用打印语句和计时器来检查我的工作,这是我发现的:
- 延时为 0.00076 秒,这显然不是预期的行为。
- 第 4 步立即显示,这又不是预期的行为。
- 显示第 1 步
- 第 2 步的显示时间为 2.92 秒,没问题。
唯一需要解决的问题是将第 4 步放在最后,因为这意味着第 1 步和第 2 步已完成。
这个函数在classA:
static func loadDataAndSignIn(completion: @escaping () -> () ) {
let start = Date()
Auth.auth().signInAnonymously { (result, error) in
if result != nil {
print("Step 1 -> Signed In. UserId is: \(result?.user.uid)")
Variables.getDataFromFirestore {
let end = Date()
print("Step 2 - > Data Fetched")
print("Data Fetched in \(end.timeIntervalSince(start)) seconds")
}
} else {
print(error?.localizedDescription ?? print("Sign in Error"))
}
}
DispatchQueue.main.async {
completion()
print("Step 4 -> Signed In and Data Loaded")
}
}
这个函数也在classA:
static func getDataFromFirestore(completion: (() -> Void)? = nil) {
let db = Firestore.firestore()
db.collection("Offences").getDocuments { (snapshot, error) in
if let error = error {
print("Error getting documents: \(error.localizedDescription)")
} else {
Variables.offencesArray.removeAll()
for offence in snapshot!.documents {
let offenceName = offence.get("name") as! String
let offencePoints = offence.get("points") as! Int
let offenceCost = offence.get("cost") as! Int
let offenceSection = offence.get("section") as! String
let offenceCategory = offence.get("category") as! String
let offenceObject = Offence(section: offenceSection, name: offenceName, cost: offenceCost, points: offencePoints, category: offenceCategory)
Variables.offencesArray.append(offenceObject)
}
}
DispatchQueue.main.async {
completion?()
}
}
}
这是在 class B:
的 viewDidLoad 中 let start = Date()
Variables.loadDataAndSignIn {
self.removeLoadingScreen()
let end = Date()
print("Time lapsed: \(end.timeIntervalSince(start))")
}
需要帮助才能让我的 threading/completion 块恢复原状。
将对 completion
的调用放入 getDataFromFirestore
的完成处理程序中,它位于 signInAnonymously
的完成处理程序中:
static func loadDataAndSignIn(completion: @escaping () -> () ) {
Auth.auth().signInAnonymously { (result, error) in
if result != nil {
print("Step 1 -> Signed In. UserId is: \(result?.user.uid)")
Variables.getDataFromFirestore {
let end = Date()
print("Step 2 - > Data Fetched")
print("Data Fetched in \(end.timeIntervalSince(start)) seconds")
DispatchQueue.main.async {
completion()
print("Step 4 -> Signed In and Data Loaded")
}
}
} else {
print(error?.localizedDescription ?? "Sign in Error")
}
}
}
注意:如果对 signInAnonymously
的调用中的 result
为零,您可能还想调用完成,例如:
[...]
} else {
print(error?.localizedDescription ?? "Sign in Error")
DispatchQueue.main.async {
completion()
}
}