在PhoneGap插件中保留回调上下文? [英] Keep callback context in PhoneGap plugin?
问题描述
我需要实现一些功能,在一个间隔触发一个动作,并将结果发送回javascript。
为了简化,我将使用PhoneGap文档中的echo示例:
- (void)echo:(CDVInvokedUrlCommand *)命令
{
[self.commandDelegate runInBackground:^ {
CDVPluginResult * pluginResult =零;
NSString * echo = [command.arguments objectAtIndex:0];
if(echo!= nil&& [echo length]> 0){
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
我想让这个调用与echo 我创建了一个定时器,每秒钟调用另一个函数,但我不知道如何保持回调的上下文发送结果。 任何帮助保持这个在回调的上下文中将是巨大的。非常感谢! 您会想要: 作为实例级变量(在任何方法之外,因此它保留其值)。首次进入插件代码时设置:
//开始计时
- (void)start:(CDVInvokedUrlCommand *)command
{
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(action :)
userInfo:nil
重复:YES];
}
//调用的操作
- (void)action:(CDVInvokedUrlCommand *)command
{
[self.commandDelegate runInBackground:^ {
NSLog(@TRIGGERED);
}];
}
NSString * myCallbackId;
myCallbackId = command.callbackId;然后,在实例化一个PluginResult之后,但在使用它之前,做一些类似的操作:
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
这会让回调有效以备将来使用。
然后执行如下操作:
[self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];
I need to implement some functionality that triggers an action on an interval and emits the results back to javascript.
To simplify things I will use the echo example from the PhoneGap documentation:
- (void)echo:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
I want to make this call the same callback with the echo every second until stop
is called.
I've created a timer that calls another function every second, but I don't know how to keep the context of the callback to send the result to.
//Starts Timer
- (void)start:(CDVInvokedUrlCommand*)command
{
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(action:)
userInfo:nil
repeats:YES];
}
//Called Action
-(void)action:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
NSLog(@"TRIGGERED");
}];
}
Any help keeping this within the context of the callback would be great. Thanks!
You'll want to have something like:
NSString *myCallbackId;
as an instance-level variable (outside of any method, so it retains its value). Set it when you first come in to the plugin code:
myCallbackId = command.callbackId;
Then, right after you instantiate a PluginResult, but before using it, do something like:
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
That will tell it to keep the callback valid for future use.
Then do something like:
[self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];
这篇关于在PhoneGap插件中保留回调上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!