2016-04-17 11 views
0

私は非常にシンプルなコードバプラグインをテストして、繰り返しコールバックをデモンストレーションしようとしています。要点は、これらの比較的単純なタスクをどのように行うかを学び、それらに自信を得ることです。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で同じ機能を持っている必要がありますが、多分窓の電話。

私はこれまでこれを持っていましたが、繰り返しメッセージをすぐに吐き出しました。テストする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.

+0

これをどのように最後に解決しましたか? – Nikola

+0

私は決してしなかった。 :( –

答えて

0

は、それはあなたがここを求めているものは不明だが、あなたのAndroidの例の行動を複製しようとしている場合、あなたはしていますちょうど睡眠行動が欠けている。 This question about delaying execution in Objective Cが役立ちます。

[NSThread sleepForTimeInterval: delay]; 

for-loopの先頭には、レスポンスが速やかに返送されないようにする必要があります。しかし、これはメインのアプリケーションスレッドをブロックするので、最も洗練されたソリューションではありません。

関連する問題