本文介绍了来自Cordova iOS插件的后续回调不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我遇到麻烦,立即启动从cordova插件(iOS一)后续回调。 在XCode调试器中,我清楚地看到它的步骤 [self.commandDelegate sendPluginResult:pluginResult callbackId:monitoredRegions.callbackId] ; 多次(立即连续),每次使用稍微不同的 pluginResult 。 由触发 [locationManager requestStateForRegion:region]; 我不能(也不能控制)。 问题是,在Javascript端,回调只会被触发一次。 什么是什么(实际上不是这样)奇怪的是,如果我介绍在该回调中的一些阻塞,如 alert(),回调被触发多次(如预期的)。这很酷,但我不需要任何 alert()在应用程序。 如果我明白了, commandDelegate 应该处理线程和排队,但它似乎没有。问题是,如何在每次调用 sendPluginResult 时调用JS回调。 p> 解决方案所以,问题不在线程/排队。 原来,你只能使用 callbackId 一次除非你告诉Cordova不要清除 callbackId 将 CDVPluginResult.keepCallback 设置为 true 。请注意 keepCallback 不是 BOOL 属性,因此您可能需要调用 [ pluginResult setKeepCallbackAsBool:YES]; CDVPluginResult * pluginResult = [sendPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:key] ; [pluginResult setKeepCallbackAsBool:YES]; //这里我们告诉Cordova不要清除sendPluginResult()后的回调ID [self.commandDelegate sendPluginResult:pluginResult callbackId:monitoredRegions.callbackId];什么混乱的是,如果你引入一个阻塞调用,如 alert()。 在该回调中,Cordova会让你用同样的 callbackId 连续多个 sendPluginResult 。 请记住,您可能需要 sendPluginResult 与 CDVPluginResult 其中 keepCallback 是 false 以释放 callbackId 你不再需要了。 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! 解决方案 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. 这篇关于来自Cordova iOS插件的后续回调不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 14:26