Error registering javascript callbacks on IOS (Android works fine)

33 views
Skip to first unread message

ka...@bitxenio.com

unread,
Jan 3, 2018, 8:09:13 AM1/3/18
to CodenameOne Discussions
Hello:

    Since the last codename1 libary update we've been having problems with the BrowserComponent on iOS. We've narrowed down the problem to the callback registration process (which gets executed on the BrowserComponent onLoad callback). We register two functions, but only the second one can be invoked from javascript. The first one gets registered ok (we can see its code from the javascript side) but fails to execute on iOS (in Android and the simulator works fine). We tried a lot of permutations, and different argument lengths for both functions, but the only difference we see is the registration order. If we switch the registration order, we can only invoke the other one (whichever gets registered last).

/*
* Registramos el callback JS de valor cambiado del checkbox "No volver a mostrar este asistente". 
*/
JSObject checkboxClickedCallback = (JSObject) javascriptContext.get("{}");
checkboxClickedCallback.set("setChecked", new JSFunction() {
public void apply(JSObject self, Object[] args) {
Log.p("->checkboxClickedCallback.setChecked()");
contexto.mostrarMensaje("checkboxClickedCallback.setChecked()");

}
});
javascriptContext.set("window.checkboxClickedCallback", checkboxClickedCallback);

/*
* Registramos el callback JS de click en el botón "OK".
*/
JSObject okButtonClickedCallback = (JSObject) javascriptContext.get("{}");
okButtonClickedCallback.set("showMap", new JSFunction() {
public void apply(JSObject self, Object[] args) {
Log.p("->okButtonClickedCallback.showMap()");
contexto.mostrarMensaje("okButtonClickedCallback.showMap()");

}
});
javascriptContext.set("window.okButtonClickedCallback", okButtonClickedCallback);

    Please help, as it's impacting our current production code.

Kind regards,
     Kandy

Steve Hannah

unread,
Jan 3, 2018, 8:47:04 AM1/3/18
to codenameone...@googlegroups.com
This looks like a race condition that has been there for a while.  I have just committed a fix for it here

With the last update, however, the com.codename1.javascript package is now deprecated in favour of a new async API that is incorporated directly into the BrowserComponent class.  I recommend migrating over to this new API for new code, as it will produce much better performance (no hidden invokeAndBlock under the hood - except in xxxAndWait() methods).

Using the new API, your code would become:

bc.addJSCallback("window.checkfoxClickedCallback ={setChecked: function(){ callback.onSuccess(null)}}", r->{
    Log.p("->checkboxClickedCallback.setChecked()");
    contexto.mostrarMensaje("checkboxClickedCallback.setChecked()");
});
bc.addJSCallback("window.okButtonClickedCallback = {showMap: function() { callback.onSuccess(null)}}", r->{
     Log.p("->okButtonClickedCallback.showMap()");
     contexto.mostrarMensaje("okButtonClickedCallback.showMap()");
});

Notice, with this new syntax, you reference the java callback explicitly in the javascript code via the "callback" variable, which, in Javascript, contains 2 methods: onSuccess(arg), and onError(message, code).

Steve




--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/493a9217-0a4a-44b5-aede-974096f652d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

ka...@bitxenio.com

unread,
Jan 3, 2018, 11:35:05 AM1/3/18
to CodenameOne Discussions
Hello:

    How can we use that new interface? There's no documentation and our current lib doesn't expose said addJSCallback() yet (and updating c1 libs didn't help). Also, is the fix already available in the build servers? I should emphasize our code stopped working two or three days ago, so I'm not sure about the race condition been there for so long.

Kind regards,
    Kandy.

Steve Hannah

unread,
Jan 3, 2018, 11:44:23 AM1/3/18
to codenameone...@googlegroups.com
On Wed, Jan 3, 2018 at 8:35 AM, <ka...@bitxenio.com> wrote:
Hello:

    How can we use that new interface? There's no documentation and our current lib doesn't expose said addJSCallback() yet (and updating c1 libs didn't help).
You need to update your project libs.  It should be there (just tested on a project here).  (I.e. Project Properties > Update Project Libs .. not "update cn1libs")
 
Also, is the fix already available in the build servers? 
No.  It will be up in the next build server update on Friday.

Steve
 

Kind regards,
    Kandy.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.

ka...@bitxenio.com

unread,
Jan 3, 2018, 1:02:11 PM1/3/18
to CodenameOne Discussions
Hi Steve

I already did the project libs update, but didn't help. My library date is 30/12/2017, when I do the update it says "Your proyect Libs are up to date", we are blocked on the development of our App to IOS.

Please help us.

Best Regards
Kandy

Kind regards,
    Kandy.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/493a9217-0a4a-44b5-aede-974096f652d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/0aa6820b-d038-4a3f-82ad-c45fb4c6d5e6%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Steve Hannah

unread,
Jan 3, 2018, 1:09:50 PM1/3/18
to codenameone...@googlegroups.com
I just confirmed with a fresh project.  The changes are there.  I'm not sure why you're not picking them up.  Is your project using the libs at the default location (lib/CodenameOne.jar and JavaSE.jar) or have you adjusted the classpath for the project.

Steve


Kind regards,
    Kandy.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.

ka...@bitxenio.com

unread,
Jan 3, 2018, 2:05:56 PM1/3/18
to CodenameOne Discussions
Hi Steve:

   Indeed, upon further inspection you're so right! We've just detected a problem with our SVN workflow in which we keep updating the "libVersion" property on codenameone.properties between dev machines WITHOUT updating the libs at the same time, so we were keeping old libs and preventing the proper update procedure.

   We now see the deprecated warnings and the new code in BrowserComponent. Will work from here.

Kind regards,
    Miguelanxo. 

Kind regards,
    Kandy.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/f7466bae-c35d-4baf-a776-ecc9e13f65ea%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

ka...@bitxenio.com

unread,
Jan 4, 2018, 9:12:10 AM1/4/18
to CodenameOne Discussions
Hi Steve

where can I found documentation or examples to addJSCallback?

I can be able to:

call from javascritp to java

browserComponent.addJSCallback(
"window.app = {isActive: function() {callback.onSuccess()}}",
value -> {
call from java to javacript and send parameters

browserComponent.execute("controlPopUp ('"+ relojReservaValueString +"', "+ reservationState +","+ vehicleLockState +")");


Now I need,

1) call from Javascritp to java and receive a response from java

2) call from java to javascript and receive a response from javascritp


Can you help me ?

Best Regards Kandy

Steve Hannah

unread,
Jan 4, 2018, 9:53:19 AM1/4/18
to codenameone...@googlegroups.com
The best examples right now are in the GoogleMaps cn1lib

There are also samples in the unit tests:

The unit tests all use the executeAndWait() wrappers for convenience, but these carry the baggage of invokeAndBlock - to make it synchronous.  Best for performance to try to use async versions whenever possible.

1) call from Javascritp to java and receive a response from java

First add your callback:
bc.addJSCallback("window.app.mycb=function(arg){callback.onSuccess(arg)}", arg->{
     // Log.p("The result was "+arg.getInt());
}); 

Then on the javascript side, you can call "window.app.mycb(1+2)", and your java callback will be called.

2) call from java to javascript and receive a response from javascript

Async style:

bc.execute("callback.onSuccess(1+2)", res->{
     Log.p("The result was "+res.getInt());
}); 

Sync Style:

int result = bc.executeAndWait("callback.onSuccess(1+2)").getInt();


If you need to pass parameters to your javascript code, there are variants that do that and handle escaping strings properly.

e.g.

bc.execute("callback.onSuccess(${0}+${1})", new Object[]{1, 2}, res->{
    Log.p("The result was "+res.getInt());  // Should be 3
});

If you need to call methods on a Javascript object, you can also create a java "Proxy" object to make this a bit easier.  E.g.

JSProxy window = bc.createJSProxy("window");
int height = window.getAndWait("innerHeight").getInt();


Steve

    Miguelanxo. 

Kind regards,
    Kandy.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.

ka...@bitxenio.com

unread,
Jan 4, 2018, 2:24:11 PM1/4/18
to CodenameOne Discussions
Thanks Steve

on the point 1 I need to call from Javascript to Java, and return from Java to Javascript a response.

My doubt is how do I have to do it in Java and in Javascritp, I searched examples in code of GoolgeMaps Implementation but I didn't find any help.

Best Regards Kandy
    Miguelanxo. 

Kind regards,
    Kandy.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/014fa1a2-ef58-49ba-bf93-546a95896146%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Steve Hannah

unread,
Jan 4, 2018, 3:04:09 PM1/4/18
to codenameone...@googlegroups.com
Calls from javascript to java must be asynchronous.   The way to do this is to:

// You want javascript to be able to call into java AND receive a result.
// From the Java side, you would add a callback
bc.addJSCallback("window.myfunc=function(arg){callback.onSuccess(arg)}", res->{
     String[] parts = Util.split(res.toString(), " ");
     String arg = parts[0];
     String callbackFunc = parts[1];
     String result = doSomethingWithArg(arg);
     bc.execute(callbackFunc+"("+result+")");
});

// On the javascript side you could then do something like this

window.myCallback = function(arg) {
   // Do something with the argument
}
myfunc("myarg window.myCallback");

This is a simplistic example that passes a a message from javascript to java by placing the "contents" of the message at the beginning, and then a callback to call in javascript after the space.  If you need to send arbitrary strings, then this scheme will fail - but you could just as easily send a JSON message from javascript that includes some content and information about where to send it (the callback information), which would be more resilient to argument formatting.

Steve

    Miguelanxo. 

Kind regards,
    Kandy.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.

ka...@bitxenio.com

unread,
Jan 4, 2018, 4:03:10 PM1/4/18
to CodenameOne Discussions
Thanks Steve.

is possible to make a synchronous call from javascript to java ?

Best Regards Kandy

    Miguelanxo. 

Kind regards,
    Kandy.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.



--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/04c278eb-ece6-40bc-8135-8dbaefd4d7c7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Steve Hannah

unread,
Jan 4, 2018, 4:14:02 PM1/4/18
to codenameone...@googlegroups.com
On Thu, Jan 4, 2018 at 1:03 PM, <ka...@bitxenio.com> wrote:
Thanks Steve.

is possible to make a synchronous call from javascript to java ?

No.  Javascript is single-threaded and it runs on its own thread.  There is no way to do a synchronous call out of Javascript without locking it up.

Best regards

Steve
Reply all
Reply to author
Forward
0 new messages