I have the following Swift code:
func callback(_ status: PHAuthorizationStatus) {
}
func myFunction() {
let handler:(PHAuthorizationStatus) -> Void = { (status) in
self.callback(status)
}
PHPhotoLibrary.requestAuthorization(for: .addOnly) { (status) in
handler(status)
}
}
What my doubt is whether there is a retain cycle in closures declared as local variable and whether we should use unowned or weak self instead inside handler. How does Swift handle closures declared as local var inside function?
PHPhotoLibrary->handler->self.selfdoesn't retainPHPhotoLibrary, so there shouldn't be an issue. And besides,PHPhotoLibraryis only likely to retainhanlderuntil it inevitably calls it, so even if there was a retain cycle, it looks like it'll be temporary and will go away on its own oncerequestAuthorizationfinishes its job. But to be sure, you would have to inspect this in the memory debugger and Instruments - Alexander