Return an object only when the method is inside it

I am using Parse.com as my backend and want to create a method that returns the columns of the requested objects as a Bundle. I am having problems with the refund, but how can I delay the refund until the request is complete? I would like to put it in a handled request method, but that is not allowed with its void method. Here's my code:

public static Bundle queryForExtras() {

    final Bundle extras = new Bundle();

    ParseQuery<Song> query = ParseQuery.getQuery(Song.class).fromPin(PIN_LABEL_SONG);
    query.getFirstInBackground(new GetCallback<Song>() {
        @Override
        public void done(final Song song, ParseException e) {

            if (e == null) {
                extras.putString(COLUMN_SONGNAME, song.getSongname());
                extras.putString(COLUMN_LYRICS, song.getLyrics());

                // I WANT TO RETURN EXTRAS HERE

            } else {
                // Something went wrong
            }
        }
    });

    return extras;
}

      

How can I achieve the desired behavior?

+3


source to share


1 answer


As getFirstInBackground

obviously done in the background, you cannot directly return a value for a method queryForExtras

. If you want to use getFirstInBackground

, you need to create some kind of listener that is called after receiving the package.

However, the simplest way would be to use a method getFirst

that doesn't run in the background. This way you can directly get the result in queryForExtras

:



public static Bundle queryForExtras() {
    ParseQuery<Song> query = ParseQuery.getQuery(Song.class).fromPin(PIN_LABEL_SONG);
    Song song = query.getFirst();

    final Bundle extras = new Bundle();
    extras.putString(COLUMN_SONGNAME, song.getSongname());
    extras.putString(COLUMN_LYRICS, song.getLyrics());
    return extras;
}

      

+4


source







All Articles