Ionic Launch Event. September 28th 2021.
In most cases, a plugin method will get invoked to perform a task and can finish immediately. But there are situations where you will need to keep the plugin call available so it can be accessed later.
Two reasons you might need a plugin call (
CAPPluginCall on iOS or
PluginCall on Android) to persist outside of the method in your plugin are:
reject() is called once. But if your method is a callback that will
resolve() multiple times, then there is an extra step involved. More information about how to declare callbacks
can be found here.
If you need to save a call to be completed once in the future, you have two options. One option is to simply save it locally in an instance variable. The second is to use the bridge’s set of methods to save it and then retrieve it later via the
callbackId. After calling
reject(), you can dispose of the call object as it will no longer be relevant (don’t forget to call
releaseCall() if you used
func saveCall(_ call: CAPPluginCall) func savedCall(withID: String) -> CAPPluginCall? func releaseCall(_ call: CAPPluginCall) func releaseCall(withID: String)
void saveCall(PluginCall call) PluginCall getSavedCall(String callbackId) void releaseCall(PluginCall call) void releaseCall(String callbackId)
Saving a call to be completed multiple times in the future means two things: saving the native call object itself (as above) and telling the bridge to preserve its bookkeeping so
reject() can be invoked repeatedly.
To mark a call this way, set its
keepAlive property (this was called
isSaved prior to version 3 but has been renamed to make the behavior clearer).
call.keepAlive = true
keepAlive is true, then
resolve() can be called as many times as necessary and the result will be returned as expected. Setting this flag to true also means that the bridge will automatically call
saveCall() for you after your method returns.