Skip to content

Instantly share code, notes, and snippets.

@carlosmuvi
Created December 11, 2014 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlosmuvi/1c522448d59a944393a9 to your computer and use it in GitHub Desktop.
Save carlosmuvi/1c522448d59a944393a9 to your computer and use it in GitHub Desktop.
bolts task example
package carlos.munoz.sports.domain;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import bolts.Continuation;
import bolts.Task;
public class GeocoderDataSource {
Context context;
public GeocoderDataSource(Context context) {
this.context = context;
}
public void findAddresses(String query){
findAddressesAsync(query).continueWith(new Continuation<List<Address>, Void>() {
@Override
public Void then(Task<List<Address>> task) throws Exception {
if (task.isCancelled()) {
// Task cancelled
} else if (task.isFaulted()) {
// task failed
Exception error = task.getError();
} else {
// the object was saved successfully.
List<Address> addresses = task.getResult();
}
return null;
}
});
}
private Task<List<Address>> findAddressesAsync(String query) {
Task<List<Address>>.TaskCompletionSource taskCompletion = Task.create();
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(query, 4);
} catch (IOException e) {
taskCompletion.setError(e);
}
if (addresses != null && addresses.size() > 0) {
taskCompletion.setResult(addresses);
} else {
taskCompletion.setResult(null);
}
return taskCompletion.getTask();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment