Hello, I am trying to wrap my first library for B4J and I do not get when to use ba.raiseEvent or ba.raiseEventFromDifferentThread...
I am trying to raise some events from different Listeners and also in some interfaces something like:
In the above example I am calling a sub name from b4j which is exposed as an "event" and all the logic (comparing) is made there. As I need it to return a value I am using raiseEvent ... also tryed with relfection (same good result) but I think it is best to use b4j internal methods (as I am writing a library for b4j) when calling a sub that does the processing and returns a result - are they any other advantages using ba.raiseEvent instead of reflection !?
In the second example I am using raiseEventFromDifferentThread because raiseEvent throws some error (NullPointerException). Using raiseEventFromDifferentThread works great.
So as far as I figured it out it's all about what the user does in the b4j sub this leads me to another question is it safe to change all my code to use ba.raiseEvent and let the user deal with it (he can use CallSubDelayed in the event sub if he encounters errors) ?
I just don't get (from mine perspective - library wrapper writer) which method to use....
I am trying to raise some events from different Listeners and also in some interfaces something like:
B4X:
import java.util.Comparator;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.CheckForReinitialize;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
@ShortName("ComparatorObject")
@Events(values={"Compare(Object1 As Object, Object2 As Object) As Int"})
public class ComparatorObjectWrapper implements Comparator<Object>, CheckForReinitialize {
private String eventName;
private BA ba;
public void Initialize(BA ba, String EventName) {
this.ba = ba;
this.eventName = EventName.toLowerCase(BA.cul) + "_compare";
}
@Override
public boolean IsInitialized() {
return (this.ba != null);
}
@Hide
@Override
//raised on another thread than the OwnersThread
public int compare(Object o1, Object o2) {
return (int) ba.raiseEvent(this, eventName, new Object[] {o1, o2});
}
}
B4X:
@ShortName("MessageListener")
@Events(values={"OnMessage(Message As Object)"})
public class MessageListenerWrapper implements MessageListener<Object>, CheckForReinitialize {
private String eventName;
private BA ba;
public void Initialize(BA ba, String EventName) {
this.ba = ba;
this.eventName = EventName.toLowerCase(BA.cul) + "_onmessage";
}
@Override
public boolean IsInitialized() {
return (this.ba != null);
}
@Hide
@Override
//raised on another thread than the OwnersThread
public void onMessage(Message<Object> message) {
ba.raiseEventFromDifferentThread(this, null, 0, eventName.toLowerCase(BA.cul), false, new Object[] {message.getMessageObject()});
}
}
So as far as I figured it out it's all about what the user does in the b4j sub this leads me to another question is it safe to change all my code to use ba.raiseEvent and let the user deal with it (he can use CallSubDelayed in the event sub if he encounters errors) ?
I just don't get (from mine perspective - library wrapper writer) which method to use....