简体   繁体   English

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

[英]Completion block is getting triggered even before my operation completes in main method

I am trying to create user in firebase using OperationQueue and Operation.我正在尝试使用 OperationQueue 和 Operation 在 firebase 中创建用户。 I placed the Firebase Auth call in operation main method.我将 Firebase Auth 调用放在操作主方法中。 Completion block of the operation is getting triggered even before firebase registration process is succeeded.即使在 firebase 注册过程成功之前,操作的完成块也会被触发。

RegistrationViewModal.swift注册ViewModal.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)

UserRegistrationOperation.swift 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

        }
    }

}

The problem is that your operation is initiating an asynchronous process, but the operation finishes when the asynchronous task is started, not when the asynchronous task finishes.问题是您的操作正在启动一个异步过程,但操作在异步任务启动时完成,而不是在异步任务完成时完成。

You need to do the KVO associated with a “concurrent” operation, as outlined in the documentation :您需要执行与“并发”操作相关的 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.在并发操作中,您的start()方法负责以异步方式启动操作。 Whether you spawn a thread or call an asynchronous function, you do it from this method.无论您是生成线程还是调用异步 function,您都可以通过此方法进行操作。 Upon starting the operation, your start() method should also update the execution state of the operation as reported by the isExecuting property.开始操作后,您的start()方法还应更新isExecuting属性报告的操作的执行 state。 You do this by sending out KVO notifications for the isExecuting key path, which lets interested clients know that the operation is now running.您可以通过为isExecuting键路径发送 KVO 通知来做到这一点,这让感兴趣的客户端知道该操作正在运行。 Your isExecuting property must also provide the status in a thread-safe manner.您的isExecuting属性还必须以线程安全的方式提供状态。

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.在完成或取消其任务后,您的并发操作 object 必须为isExecutingisFinished键路径生成 KVO 通知,以标记您的操作对 state 的最终更改。 (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. (在取消的情况下,更新isFinished键路径仍然很重要,即使该操作没有完全完成其任务。排队的操作必须报告它们已完成,然后才能从队列中删除。)除了生成 KVO 通知时,您对isExecutingisFinished属性的覆盖也应该继续根据您的操作的 state 报告准确的值。

Now all of that sounds quite hairy, but it's actually not that bad.现在所有这些听起来都很毛茸茸,但实际上并没有那么糟糕。 One way is to write a base operation class that takes care of all of this KVO stuff, and this this answer outlines one example implementation .一种方法是编写一个基本操作 class 来处理所有这些 KVO 内容,这个答案概述了一个示例实现

Then you can subclass AsynchronousOperation instead, and make sure to call finish (or whatever triggers the isFinished KVO) when the task is done:然后您可以改为子类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
        }
    }
}

There are lots of ways to implement that AsynchronousOperation class and this is just one example.有很多方法可以实现AsynchronousOperation class,只是一个例子。 But once you have a class that nicely encapsulates the concurrent operation KVO, you can subclass it and you can write your own concurrent operations with very little changes to your code.但是一旦你有一个 class 很好地封装了并发操作 KVO,你就可以将它子类化,并且你可以编写自己的并发操作,只需对代码进行很少的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM