0

I have some problems with getting data from ion. I need to get variable from function and i know that this function ion is performed separately from the main stream. How can i get this data? Maybe i need to wait while function doesn't completed, but how can i do it?

final String someText;
    Ion.with(this)
           .load(URL)
            .asString()
            .setCallback(new FutureCallback<String>() {
                @Override
                public void onCompleted(Exception e, String result) {
                    someText = result.toString();
                    System.out.println(someText) // i get someText not empty
                }
            });
System.out.println(someText) // i get someText empty
DamirKH
  • 1
  • 1

1 Answers1

0

Ion is asynchronous, so someText has not been set by the callback. If you need to block to wait for the result, you can call .get() instead of using setCallback.

final String someText = Ion.with(this)
       .load(URL)
        .asString()
        .get(); // THIS WILL BLOCK THE THREAD
System.out.println(someText) // i get someText not empty

I'd not recommend doing that, because then you would block the calling thread, which may be the main thread. Using setCallback is preferred.

koush
  • 2,972
  • 28
  • 31