Re: Swift closure isn't allowed to capture context

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Stefan

An approach that worked for me when working with callbacks was to pass the information received from the callback to a static method on a dispatcher class that would handle the processing of the data. My idea was to get out of the callback as fast as possible and do the heavy lifting in a real Swift environment.

For example, for the call state callback:

uaConfig.memory.cb._on_call_state_ = { (callId, event) -> Void in
    Dispatch.dispatch({ () -> Void in Dispatch.dispatchCallState(callId, event: event) })
}

Where Dispatch.dispatch() just executes the closure that I pass to it on a serial queue.

Dispatch.dispatchCallState() is a static function that collects all the relevant information it receives and passes it down to a Call swift object. You can use here a notification, a call registry, whatever is most convenient.

Alin


On Sat, Nov 12, 2016 at 10:31 PM, Stefan Godoroja <mancunianetz@xxxxxx> wrote:
Hi guys,

Recently I was trying to use pjsip stack libraries in an iOS project which uses Swift language, but I have noticed
a thing which makes library hard to use. C function pointers are translated into Swift closures, a special type of closure. The problem is
that Swift doesn’t allow to capture context (self) in this type of closure. Looking into the source code of pjsip libs convinced me that
pjsip callbacks don’t have a parameter for referencing context. I’m not a C specialist but this seems to me at least not ok.

I’m addressing the developers who used library in Swift projects, how did you handle this situation ?



Best regards,
Stefan




_______________________________________________
Visit our blog: http://blog.pjsip.org

pjsip mailing list
pjsip@xxxxxxxxxxxxxxx
http://lists.pjsip.org/mailman/listinfo/pjsip_lists.pjsip.org

_______________________________________________
Visit our blog: http://blog.pjsip.org

pjsip mailing list
pjsip@xxxxxxxxxxxxxxx
http://lists.pjsip.org/mailman/listinfo/pjsip_lists.pjsip.org

[Index of Archives]     [Asterisk Users]     [Asterisk App Development]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [Linux API]
  Powered by Linux