在NSOperation中调用init时,Swift 3添加@esaping属性崩溃

时间:2017-05-25 12:18:06

标签: swift3 ios10

我有一个定义了这样的闭包的函数:

func synchronizeData(completion  externalCompletion: RequestsCompletionHandler?) {

    let closure = {

        (operationCompletion:@escaping ()->Void) in

        assert(Thread.isMainThread, "must be the main thread")

        /*
        Internal (non-optional) completion handler
        */
        let internalCompletion: RequestsCompletionHandler = {

            (success, newData, decommissionRequired, errors) -> Void in

            /*
            Synchronization is finished. Firstly, call either the external completion handler or the delegate
            */
            if let externalCompletion = externalCompletion {

                externalCompletion(success, newData, decommissionRequired, errors)
            }
            else {

                self.delegate?.synchroniationInteractor(self, didSynchronizeDataWithStatus: success, dataIsNew: newData, decommissionRequired: decommissionRequired, error: errors.last)
            }

            /*
            Now call the closure operation's completion handler
            */
            operationCompletion()
        }

        /*
        The synchronization itself
        */
        guard let _ = self.keychain.retrieveActivationIdentifiers() else {

            internalCompletion(false, false, false, [NSError(domain: "", code: 0, userInfo: ["reason" : "unable to retrieve credentials"])])
            return
        }

        var errors :[NSError] = []

        var numberOfRequests = 0
        var completedRequests = 0

        var decommissionRequired: Bool?

        /*
        Synchronization results handler. Regardless of success for fail, increase completed requests counter and append any errors to array, if final request call main completion block
        */
        let handleCompletedRequests = {

            (error: NSError?) -> Void in

//          assert(NSThread.isMainThread(), "must be the main thread")

            if let error = error {
                errors.append(error)
            }

            completedRequests += 1

            if(completedRequests >= numberOfRequests) {

                internalCompletion(errors.count == 0, true, decommissionRequired, errors)

                /*
                Decrement operations counter
                */
                self.manageBusy(retain: false)
            }
        }

        /*
        Increment operations counter
        */
        self.manageBusy(retain: true)

        /*
        Do the actual synchronization.
        Fetch the Patient Data first
        */
        self.fetchPatientDataInternal {

            (success, newData, error) -> Void in

            numberOfRequests = 6

            //Fetch Patient Schedule
            self.fetchPatientScheduleInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }

            //Fetch Patient Thresholds
            self.fetchPatientThresholdInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }

            //Fetch Patient Device Settings
            self.fetchPatientDeviceSettingsInternal {

                (success, newData, decommissionReq, error) -> Void in

                decommissionRequired = decommissionReq
                handleCompletedRequests(error)
            }

            // Device Checkin
            self.deviceCheckInInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }

            // Upload Vitals
            self.uploadPendingVitalsInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }

            //Upload Health sessions
            self.uploadPendingHealthSessionsInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }
        }
    }
    let operation =  CIAsyncronousOperation.init(closure: closure as! (()->Void)-> Void)
    operation.name = "Data Synchronization"
    isolationQueue.addOperation(operation)
}

当我们在上面的函数中调用这一行时,即

let operation =  CIAsyncronousOperation.init(closure: closure as! (()->Void)-> Void)

应用程序崩溃并显示以下信息:

  

0x00000001003b083c CIAppliance`partial适用于CIAppliance.SynchronizationInteractor的转发器。(synchronizeData(完成:Swift.Optional<(Swift.Bool,Swift.Optional,Swift.Optional,Swift.Array< __ ObjC.NSError>) - >() >) - >())。(闭包#1)在SynchronizationInteractor.swift

CIAsyncronousOperation init定义如下:

init(closure aClosure: @escaping (()->Void)-> Void)
{

        closure = aClosure
}

我无法找出崩溃的原因。是铸造问题还是由于新的Swift 3语法更改?

1 个答案:

答案 0 :(得分:0)

如果您必须强制执行此功能,则很可能您的功能签名不正确。如果CIAsyncronousOperation定义如下,它将编译。作为参数aClosure的函数和函数都需要设置为@escaping

class CIAsyncronousOperation {
    init(closure aClosure: @escaping (@escaping ()->Void)->Void)
    {
        closure = aClosure
    }
    var closure : (@escaping ()->Void)-> Void;
}