ios - 甚至在我的操作在 main 方法中完成之前就触发了完成 block

标签 ios swift firebase-authentication nsoperationqueue nsoperation

我正在尝试使用 OperationQueue 和 Operation 在 firebase 中创建用户。我将 Firebase Auth 调用放在操作主要方法中。甚至在 firebase 注册过程成功之前就触发了操作的完成 block 。

RegistrationViewModal.swift

//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)

用户注册操作.swift

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

        }
    }

}

最佳答案

问题是您的操作正在启动一个异步进程,但操作在异步任务启动时完成,而不是在异步任务完成时完成。

您需要执行与“并发”操作关联的 KVO,如 the documentation 中所述。 :

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, your start() method should also update the execution state of the operation as reported by the isExecuting property. You do this by sending out KVO notifications for the isExecuting key path, which lets interested clients know that the operation is now running. Your isExecuting 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 and isFinished key paths to mark the final change of state for your operation. (In the case of cancellation, it is still important to update the isFinished 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 the isExecuting and isFinished properties should also continue to report accurate values based on the state of your operation.

现在所有这些听起来很毛茸茸,但实际上并没有那么糟糕。一种方法是编写一个基本操作类来处理所有这些 KVO 内容,并且 this this answer outlines one example implementation .

然后你可以继承AsynchronousOperation,并确保在任务完成时调用finish(或任何触发isFinished KVO) :

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
        }
    }
}

有很多方法可以实现 AsynchronousOperation 类和 this只是一个例子。但是一旦你有了一个很好地封装了并发操作 KVO 的类,你就可以将它子类化,并且你可以编写自己的并发操作,而只需对代码进行很少的更改。

关于ios - 甚至在我的操作在 main 方法中完成之前就触发了完成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58280273/

相关文章:

ios - 当 View 重新进入前景时恢复绘制 CAShapeLayer?

ios - 每个单元格的背景图像

ios - 如何让 iOS 应用直接访问 Firebase 数据库并绕过规则?

android - Firestore : query all documents from a collection

ios - 为 iPad 2 进行开发

ios - 无法快速获取要在 UITextView 中显示的正确文件

swift - 使用 Swift 将数据从 UICollectionViewCell 传递到代码中的另一个 UICollectionViewCell,无需 Storyboard

ios - 无法在我的 View Controller 中看到 Collection View

ios - [NSOperationQueue mainQueue] 保证是串行的吗?

firebase - 如何使用 Firebase Cloud 功能创建用户后发送电子邮件验证?