Subsequent callbacks from a Cordova iOS plugin don't fire

北城以北 提交于 2019-11-29 14:14:09

问题


I have troubles with firing immediately subsequent callbacks from a cordova plugin (an iOS one). In XCode debugger I clearly see it steps over

[self.commandDelegate sendPluginResult:pluginResult callbackId:monitoredRegions.callbackId];

several times (in immediate succession), each time with a slightly different pluginResult. That gets triggered by

[locationManager requestStateForRegion:region];

which I don't (and can't) control.

The problem is, on the Javascript side, the callback gets fired only once.

What's (actually not so) strange is that if I introduce some blocking in that callback, like alert(), the callback gets fired several times (as expected). That's cool but I don't need any alert() in the app.

If I understand it, the commandDelegate should take care of threading and queueing, but it seems like it doesn't. The question is, how to get the JS callback fired each time sendPluginResult gets called.

Many thanks!


回答1:


So, the issue wasn't in threading/queueing.

It turned out that you are allowed to use a callbackId only once unless you tell Cordova not to cleanup that callbackId by setting CDVPluginResult.keepCallback to true. Pay attention that keepCallback isn't a BOOL property, so you may need to call [pluginResult setKeepCallbackAsBool:YES];

CDVPluginResult* pluginResult = [sendPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:key];
[pluginResult setKeepCallbackAsBool:YES]; // here we tell Cordova not to cleanup the callback id after sendPluginResult()
[self.commandDelegate sendPluginResult:pluginResult callbackId:monitoredRegions.callbackId];

What messes things up is that if you introduce a blocking call like alert() in that callback, Cordova will let you make multiple successive sendPluginResult with the same callbackId.

Keep in mind that you may need to sendPluginResult with a CDVPluginResult which keepCallback is false to release the callbackId that you don't need anymore.



来源:https://stackoverflow.com/questions/24776819/subsequent-callbacks-from-a-cordova-ios-plugin-dont-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!