如何在Cordova中创建基本的重复回调?

时间:2016-04-17 10:17:25

标签: ios cordova-plugins

我正在尝试制作一个非常简单的cordova插件来测试/演示重复的回调。关键是我要学习如何完成这些相对简单的任务,并获得他们的信任。

在Android上我正在这样做,它运行良好:

public boolean execute(...

中的

if ("repeat".equals(action)) {
    int count = args.getInt(0);
    int delay = args.getInt(1);
    String message = args.getString(2);

    this.repeat(count, delay, message, callbackContext);
    return true;
}

在同一课堂上我有

private void repeat(final int count, final int delay, final String message, final CallbackContext callbackContext) {
    new Thread(new Runnable() {
        public void run() {
            for (int i = 0 ; i < count ; i++) {
                if (i > 0) { // only delay after the first iteration. Had this been at the end of the method, we'd have had to do some extra math to keep track of the last iteration.
                    try {
                        Thread.sleep(delay);
                    } catch (InterruptedException e) {
                        //
                    }
                }
                try {
                    JSONObject json = new JSONObject();
                    json.put("message", message);
                    json.put("count", (i+1));
                    json.put("time", System.currentTimeMillis());

                    PluginResult result = new PluginResult(PluginResult.Status.OK, json);
                    result.setKeepCallback(true);
                    callbackContext.sendPluginResult(result);
                } catch (JSONException e) {
                    PluginResult result = new PluginResult(PluginResult.Status.ERROR, "JSON Error: " + e.getMessage());
                    result.setKeepCallback(true);
                    callbackContext.sendPluginResult(result);
                }
            }

            callbackContext.success();
        }
    }).start();
}

现在,为了演示工作,我需要在至少iOS上拥有相同的功能,但也许还有Windows手机。

对于iOS我到目前为止已经有了这个,但它只是立刻吐出重复的消息而没有延迟,而且我没有iPhone可以测试。

- (void)repeat:(CDVInvokedUrlCommand*)command
{
    NSInteger count   = [[command.arguments objectAtIndex: 0] intValue];
    NSInteger delay   = [[command.arguments objectAtIndex: 1] intValue];
    NSString* message = [command.arguments objectAtIndex: 2];

    for (int i = 1; i <= count; i++)
    {
        NSDictionary *payload = [NSDictionary dictionaryWithObjectsAndKeys:
            message, @"message", 
            @(i), @"count", 
            @"0", @"time", 
            nil];

        CDVPluginResult* pluginResult = nil;
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:payload];
        [pluginResult setKeepCallbackAsBool:YES];

        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }

    CDVPluginResult* pluginResult = nil;
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

到目前为止,这是测试插件:EventTest plugin source.

1 个答案:

答案 0 :(得分:0)

目前还不清楚你在这里要求的是什么,但是如果你试图复制Android示例的行为,那么你只是错过了睡眠行为。 This question about delaying execution in Objective C应该有所帮助。

[NSThread sleepForTimeInterval: delay];

在for循环的顶部应该足以阻止响应快速连续发送回来。但这将阻止主应用程序线程,因此它不是最优雅的解决方案。