Completion block is getting triggered even before my operation completes in main method
我正在尝试使用 OperationQueue 和 Operation 在 Firebase 中创建用户。我将 Firebase Auth 调用放在操作主方法中。即使在 Firebase 注册过程成功之前,也会触发操作的完成块。
RegistrationViewModal.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 | //This is operation initialization let operationQueues = OperationQueues() let registrationRecord = RegistrationRecord(user: self.user!, encryptedData: self.fireBaseAuthCompliance) let userRegistrationOperation = UserRegistrationOperation(registrationRecord: registrationRecord) userRegistrationOperation.completionBlock = { //I am expecting this completion block will be called only when my firebase invocation in main() method is finished DispatchQueue.main.async { //Since this block is getting triggered even before completion, the //value is returning as null self.user?.uid = userRegistrationOperation.registrationRecord.user.uid } } operationQueues.userRegistrationQueue.addOperation(userRegistrationOperation) |
UserRegistrationOperation.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | class OperationQueues { lazy var userRegistrationQueue: OperationQueue = { var queue = OperationQueue() queue.maxConcurrentOperationCount = 1 queue.name ="User registration queue" return queue }() } class UserRegistrationOperation: Operation { var registrationRecord: RegistrationRecord init(registrationRecord: RegistrationRecord) { self.registrationRecord = registrationRecord } override func main() { guard !isCancelled else { return } self.registrationRecord.state = RegistrationStatus.pending //Firebase invocation to create a user in Firebase Auth Auth.auth().createUser(withEmail: self.registrationRecord.user.userEmail, password: self.registrationRecord.encryptedData){ [weak self](result, error) in if error != nil { print("Error occured while user registration process") self?.registrationRecord.state = RegistrationStatus.failed return } self?.registrationRecord.user.uid = result?.user.uid self?.registrationRecord.state = RegistrationStatus.processed } } } |
问题是你的操作正在启动一个异步进程,但是在异步任务启动时操作完成,而不是在异步任务完成时完成。
你需要做与a€?concurrenta€?相关的KVO?操作,如文档中所述:
If you are creating a concurrent operation, you need to override the following methods and properties at a minimum:
start() isAsynchronous isExecuting isFinished In a concurrent operation, your
start() method is responsible for starting the operation in an asynchronous manner. Whether you spawn a thread or call an asynchronous function, you do it from this method. Upon starting the operation, yourstart() method should also update the execution state of the operation as reported by theisExecuting property. You do this by sending out KVO notifications for theisExecuting key path, which lets interested clients know that the operation is now running. YourisExecuting property must also provide the status in a thread-safe manner.Upon completion or cancellation of its task, your concurrent operation object must generate KVO notifications for both the
isExecuting andisFinished key paths to mark the final change of state for your operation. (In the case of cancellation, it is still important to update theisFinished key path, even if the operation did not completely finish its task. Queued operations must report that they are finished before they can be removed from a queue.) In addition to generating KVO notifications, your overrides of theisExecuting andisFinished properties should also continue to report accurate values based on the state of your operation.
现在所有这些听起来都令人毛骨悚然,但实际上并没有那么糟糕。一种方法是编写一个处理所有这些 KVO 内容的基本操作类,这个答案概述了一个示例实现。
然后你可以继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class UserRegistrationOperation: AsynchronousOperation { var registrationRecord: RegistrationRecord init(registrationRecord: RegistrationRecord) { self.registrationRecord = registrationRecord super.init() // whenever you subclass, remember to call `super` } override func main() { self.registrationRecord.state = .pending //Firebase invocation to create a user in Firebase Auth Auth.auth().createUser(withEmail: registrationRecord.user.userEmail, password: registrationRecord.encryptedData) { [weak self] result, error in defer { self?.finish() } // make sure to call `finish` regardless of how we leave this closure guard let result = result, error == nil else { print("Error occured while user registration process") self?.registrationRecord.state = .failed return } self?.registrationRecord.user.uid = result.user.uid self?.registrationRecord.state = .processed } } } |
有很多方法可以实现