2016-04-17 5 views
0

Ich versuche ein sehr einfaches Cordova-Plugin zu machen, um wiederholte Callbacks zu testen/zu demonstrieren. Der Punkt ist für mich zu lernen, wie man diese relativ einfachen Aufgaben erledigt, und Vertrauen mit ihnen zu gewinnen.Wie erstelle ich einen grundlegenden wiederholenden Rückruf in Cordova?

Auf Android Ich tue dies, und es funktioniert gut:

in 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; 
} 

Und in der gleichen Klasse I

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(); 
} 

Jetzt haben für eine Demonstration Arbeit, ich brauche die gleiche Funktion auf mindestens iOS, aber vielleicht auch Windows Phone.

Für iOS habe ich das bisher, aber es spuckt die wiederholten Nachrichten sofort ohne Verzögerung aus, und ich habe kein iPhone zum Testen.

- (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]; 
} 

Hier ist der Test-Plugin bisher: EventTest plugin source.

+0

Wie hast du das am Ende gelöst? – Nikola

+0

Ich habe es nie getan. :( –

Antwort

0

Es ist unklar, was Sie hier sind gefragt, aber wenn Sie versuchen, die Verhalten Ihres Android-Beispiel zu replizieren, dann bist du fehlt nur das Schlafverhalten. This question about delaying execution in Objective C sollte helfen.

Am Anfang Ihrer For-Schleife sollte ausreichend sein, um zu verhindern, dass die Antworten in schneller Folge zurückgeschickt werden. Dies wird jedoch den Hauptanwendungs-Thread blockieren, so dass es nicht die eleganteste Lösung sein wird.

Verwandte Themen