swift - Capturing self weak or unowned on asynchronous network requests -
whenever doing asynchronous network requests on arrival of request self nil (e.g. viewcontroller dismissed).
to prevent capturing self weak:
future.onsuccess(context: queue.main.context, callback: { [weak self] result in if let strongself = self { // stuff self, guaranteed not nil // strongself.somemethod() } })
or capture self unowned:
future.onsuccess(context: queue.main.context, callback: { [unowned self] result in // stuff self // self.somemethod() })
i not care request returning, because when request returning @ point in time when viewcontroller dismissed have nothing show request. not want keep self "alive" closure.
my question - enough capture self unowned in case? or have time nil checking [weak self]? happens in case of unowned capturing request when arrives , self nil - closure still alive , execute , trigger runtime error on accessing nil? or closure deallocated self , request run "void"?
or can forget both weak , unowned in case because when self get's deallocated closure deallocated , there no danger of accessing nil on arrival of request?
ps: know there many questions-answers on topic not find answer in case not care async arrival.
if capture self without weak
or unowned
keywords, view controller retained closure , not deallocate before closure dies. difference between weak
, unowned
weak
optional , requires conditional binding safety check. on other hand, unowned
not fail-safe. assumes view controller still alive , crash if not. more information see this answer.
Comments
Post a Comment