<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">In the class method case, the scope of your `manager` object is local the the class method and it is deallocated immediately as function returns (before the closure gets a chance to be called). <div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 24, 2017, at 12:22 PM, Nate Birkholz via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">If I use these two classes:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> MotionManager {</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> doTheThing(completionHandler: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">@escaping</span><span style="font-variant-ligatures:no-common-ligatures" class=""> (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">CMDeviceMotion</span><span style="font-variant-ligatures:no-common-ligatures" class="">?)->() ) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,38,26)" class="">"in"</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> manager = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">CMMotionManager</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> manager.</span><span style="color:rgb(52,149,175);font-variant-ligatures:no-common-ligatures" class="">deviceMotionUpdateInterval</span><span style="font-variant-ligatures:no-common-ligatures" class=""> = 0.01</span><br class=""></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> manager.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">startDeviceMotionUpdates</span><span style="font-variant-ligatures:no-common-ligatures" class="">(to: .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">main</span><span style="font-variant-ligatures:no-common-ligatures" class="">) { (motion, error) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,38,26)" class="">"out"</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> completionHandler(motion)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> OtherManager {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> manager: </span><span style="font-variant-ligatures:no-common-ligatures" class="">CMMotionManager</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">init</span><span style="font-variant-ligatures:no-common-ligatures" class="">() {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures" class="">manager</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = </span><span style="font-variant-ligatures:no-common-ligatures" class="">CMMotionManager</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures" class="">manager</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures:no-common-ligatures" class="">deviceMotionUpdateInterval</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = 0.01</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> doTheThing(completionHandler: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">@escaping</span><span style="font-variant-ligatures:no-common-ligatures" class=""> (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">CMDeviceMotion</span><span style="font-variant-ligatures:no-common-ligatures" class="">?)->() ) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,38,26)" class="">"in 2"</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">manager</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">startDeviceMotionUpdates</span><span style="font-variant-ligatures:no-common-ligatures" class="">(to: .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">main</span><span style="font-variant-ligatures:no-common-ligatures" class="">) { (motion , error) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> completionHandler(motion)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,38,26)" class="">"out 2"</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures:no-common-ligatures" class="">manager</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures:no-common-ligatures" class="">stopDeviceMotionUpdates</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> other = </span><span style="font-variant-ligatures:no-common-ligatures" class="">OtherManager</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">other</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures:no-common-ligatures" class="">doTheThing</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { (motion) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(motion)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">MotionManager</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures:no-common-ligatures" class="">doTheThing</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { (motion) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(motion)</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">
</span></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div class=""><br class=""></div><div class="">The class method on MotionManager prints "in" but never prints "out". The instance method version on. OtherManager works just fine, printing "in 2" and "out 2". Clearly the CMMotionManager doesn't survive long enough to finish its call, but I would have thought it would. </div><div class=""><br class=""></div><div class="">Is this a bug? Or am I just misunderstanding how the class method should work?<br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature">Nate Birkholz</div>
</div></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>