Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Partial apply for closure#1 in Swift

I am trying to make sense of this crash report but can not make sense, because the function "applySettings()" is not called from init() as is shown in the crash report. What is "partial apply for closure#1" in Swift?

enter image description here

Here is the desired code for init() function.

   public override init()
{
    super.init()
    
    discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera, AVCaptureDevice.DeviceType.builtInDualCamera, AVCaptureDevice.DeviceType.builtInTelephotoCamera, AVCaptureDevice.DeviceType.builtInDualWideCamera,
            AVCaptureDevice.DeviceType.builtInTripleCamera,
            AVCaptureDevice.DeviceType.builtInUltraWideCamera], mediaType: AVMediaType.video, position: .unspecified)
    
    detectLenses()
    
    checkForDeviceAuthorization()
    
    setZoomParams()
    
    sessionQueue.async { [unowned self] in
        self.configureSession()
    }

}
like image 830
Deepak Sharma Avatar asked Jul 09 '20 17:07

Deepak Sharma


2 Answers

You cannot do async stuff in an init. We are trying to return the initialized object; that is all you should be doing here. Everything else should happen in some subsequent configuration call when self completely exists.

like image 81
matt Avatar answered Sep 18 '22 01:09

matt


@DanielKaplan

I am looking for an answer to the TITLE (as is the OP), not the cause of the stack trace. The answer must explain what "partial apply for closure#1" means.

A bit of background (from here, for more deep in math):

enter image description here

So what's going on with?

sessionQueue.async { [unowned self] in
    self.configureSession()
}

Here we have closure calls function directly, swift compiler recognises this and makes currying, ie. instead of call function of function, unwraps closure and injects into async direct call of internal function, like

sessionQueue.async(execute: CapturePipeline.configureSession(self))

but to join this with source code debug info should preserve information about this simplification, so they mark it as partial apply for closure# (where N is just ordered number of existed closures in parent function).

And to fix that crash as said before the best is to remove that part from init at all and call after creation completed. The worse, but depending on other code might be applicable, is to use [weak self].

backup

like image 36
Asperi Avatar answered Sep 19 '22 01:09

Asperi