JavaFX RESTful client with DataFX

243 views
Skip to first unread message

Tai Hu

unread,
May 22, 2014, 8:31:04 PM5/22/14
to dataf...@googlegroups.com
Hi,
    Current I am building a JavaFX RESTful client with DataFX. However, I ran into a problem due to DataFX is retrieving data in the background thread. In my Login window, once user click on login, I will call the following code in my UI controller

public ObjectProperty<RestResponse> login(String userName, String password) {
    RestSource<RestResponse> restSource = new RestSource("url to my service", new JsonConverter(RestResponse.class));
    ObjectDataProvider provider = new ObjectDataProvider(restSource);
    provider.retrieve();

    return provider.getData();
}

However, since data retrieving is done in a background thread, at that moment, provider.getData() is not populated. How can I let my application pause and wait until data available?

Thanks so much for your help,

Tai

Johan Vos

unread,
May 23, 2014, 2:30:23 AM5/23/14
to Tai Hu, dataf...@googlegroups.com
Hi Tai,

What you typically do in this situation is that you add a listener to the worker property so that you get notified when the call succeeded (or failed), e.g. something like
Worker worker = provider.retrieve();
ObjectProperty<RestResponse> result = provider.getData();
worker.stateProperty.addListener(new ChangeListener<Worker.State>() {
  public void changed (Observable o, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
// and here your checks, e.g.
if (result.validlogin) {
}
}
   }
})


- Johan


--
You received this message because you are subscribed to the Google Groups "DataFX" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datafx-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hendrik Ebbers

unread,
May 23, 2014, 5:56:33 AM5/23/14
to dataf...@googlegroups.com, Tai Hu, jo...@lodgon.com
Hi,

or you can add a listener directly to the ObjectProperty<RestResponse> instance.
To unsubscribe from this group and stop receiving emails from it, send an email to datafx-dev+unsubscribe@googlegroups.com.

Tai Hu

unread,
May 23, 2014, 10:43:56 AM5/23/14
to dataf...@googlegroups.com
Thanks for your help. I think I will add a listener on my RestResponse.

Johan Vos

unread,
May 23, 2014, 10:47:59 AM5/23/14
to Tai Hu, dataf...@googlegroups.com
In that case, you have to be careful that the RestResponse itself might be created before all values on it are filled. You will get a callback when the object is created, but there is no guarantuee that the fields are already completed, and you won't be notified afterwards, unless you use properties for those fields as well.

- Johan


--
You received this message because you are subscribed to the Google Groups "DataFX" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datafx-dev+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages