Skip to content

Instantly share code, notes, and snippets.

@marcoRS
Last active August 29, 2015 14:25
Show Gist options
  • Save marcoRS/22bbdb4924b443ce490c to your computer and use it in GitHub Desktop.
Save marcoRS/22bbdb4924b443ce490c to your computer and use it in GitHub Desktop.
fetchForUser Refactored
public static void fetchForUser(final Context ctx, RJUser user, final SpotResponse listener) {
JourneyRequest.getInstance(ctx).startAsync(SpotRoutes.userSpots(user.getId()), new JourneyRequest.RequestArrayListener() {
@Override
public void onSuccess(JSONArray response) {
Realm realm = Realm.getInstance(ctx);
realm.beginTransaction();
try {
RealmList<RJSpot> spots = new RealmList<>();
for (int i = 0; i < response.length(); i++) {
RJSpot spot = jsonToSpot(response.getJSONObject(i));
RJSpot _spot = realm.copyToRealmOrUpdate(spot);
spots.add(_spot);
}
listener.onSuccess(spots);
} catch (Exception error) {
if (listener != null) listener.onFailure();
} finally {
realm.commitTransaction();
}
}
@Override
public void onFail(VolleyError volleyError) {
if (listener != null) listener.onFailure();
}
});
}
private static RJSpot jsonToSpot(JSONObject obj) throws JSONException {
RJSpot spot = new RJSpot();
spot.setId(obj.getString("id"));
spot.setName(obj.getString("name"));
spot.setVerified(obj.getBoolean("verified"));
spot.setAuth_type(obj.getString("auth_type"));
spot.setAddress(obj.getString("address"));
spot.setPhone_number(obj.getString("phone_number"));
spot.setLatitude(obj.getLong("latitude"));
spot.setLongitude(obj.getLong("longitude"));
spot.setType(obj.getInt("type"));
spot.setBadge_url(obj.getString("badge_url"));
return spot;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment