swift - Confusion Regarding How to Use Capture Lists to Avoid a Reference Cycle -


my custom uiviewcontroller subclass has stored closure property. closure signature defined take single argument of same type of class:

class myviewcontroller {      var completionhandler : ((myviewcontroller)->(void))?      // ... } 

...the idea being, object passing argument of handler, bit like uialertaction initializer.

in addition, , convenience, have factory(-ish) class method:

class func presentinstance(withcompletionhandler handler:((myviewcontroller)->(void))) {    // ... } 

...that performs following actions:

  1. creates instance of view controller,
  2. assigns completion handler property,
  3. presents modally whatever happens top/root view controller @ time of call.

my view controller leaking: set breakpoint on deinit() execution never hits it, way after i'm done view controller , dismissed.

i not sure of how or should specify capture list in order avoid cycle. every example have come across seems place closure body defined, can't code compile.

  1. where declare closure property? (how?)

    var completionhandler : ((myviewcontroller)->(void))? // if so, go? 
  2. where declare closure parameter?

    class func presentinstance(withcompletionhandler handler:((myviewcontroller)->(void))) { // again, go? 
  3. where call above function , pass closure body?

    myviewcontroller.presentinstance(withcompletionhandler:{     [unowned viewcontroller] viewcontroller in       // ... }) // ^ use of unresolved identifier viewcontroller // ^ definition conflicts previous value 
  4. where call closure, towards self? none of these compile:

    self.completionhandler?(unowned self) self.completionhandler?([unowned self] self) self.completionhandler?([unowned self], self) 

well, turns out view controller being retained block, not 1 thinking:

class myviewcontroller {     deinit(){         print("deinit...")     }      // ...      @ibaction func cancel(sender:uibutton)     {         completionhandler(self)          // view controller dismissed, ,         // deinit() gets called.          }      @ibaction func punchit(sender: uibutton)     {         mywebservice.sharedinstance.sendrequest(                 completion:{ error:nserror? in                   self.completionhandler(self)                    // view controller dismissed,                  // deinit() not called.              }         )     } 

...so closure passed mywebservice.sharedinstance.sendrequest() keeoing view controller alive. fixed adding capture list this:

mywebservice.sharedinstance.sendrequest(                 completion:{ [unowned self] error:nserror? in 

however, still don't quite understand why short-lived completion handler, passed web service class, executed once, , disposed, keeping view controller alive. closure, not stored anywhere property, should deallocated exited, right?

i must missing something. guess i'm still not thinking in portals closures.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -