tinkering with
android architectures
@nhpatt / Javier Gamarra
please,
ask questions
Context
La La App
rotation change (state)
recreates the class
DRAMA
Several dramas
Async Task
private class ...AsyncTask
extends AsyncTask<Object, Void, Object> {
protected Object doInBackground(Object... params) {
try {
return networkRequest().result
} catch (IOException e) {}
}
protected void onPostExecute(Object result) {
adapter.notifyDataSetChanged();
}
}
Memory Leak
private static class ...AsyncTask
extends AsyncTask<Object, Void, Object> {
protected Object doInBackground(Object... params) {
try {
return networkRequest().result
} catch (IOException e) {}
}
protected void onPostExecute(Object result) {
adapter.notifyDataSetChanged(); // doesn’t compile
}
}
everything in the same class
public class ...AsyncTask
extends AsyncTask<Object, Void, Object> {
protected Object doInBackground(Object... params) {
try {
return networkRequest().result
} catch (IOException e) {}
}
protected void onPostExecute(Object result) {
activity.notifyUI(locations);
}
public ListFilmLocationsAsyncTask(MainActivity mainActivity) {
this.activity = mainActivity;
}
exception
public class ...AsyncTask
extends AsyncTask<Object, Void, Object> {
protected Object doInBackground(Object... params) {
try {
return networkRequest().result
} catch (IOException e) {}
}
protected void onPostExecute(Object result) {
if (activity != null && !activity.isDestroyed() ...) {
activity.notifyUI(locations);
}
}
Several dramas
Several dramas… in Android
Solution in detail
Set a callback and don’t return anything?
public void requestPassword(... args) throws Exception {
validate(...);
Session session = new SessionImpl(...);
session.setCallback(new Callback(getTargetScreenletId()));
Service service = getService(session);
service.sendPasswordByEmailAddress(companyId, emailAddress);
}
???
public void onEvent(ForgotPasswordEvent event) {
if (!isValidEvent(event)) {return;}
if (event.isFailed()) {
getListener().error(event.getException());
} else {
getListener().success(event.isPasswordSent());
}
}
Event Bus! (2012-2013 solution)
Solution
Solution in detail
Dramas vs solutions
And the cache?
Cache...
Impossible to create a transparent solution
A mess
A mess
Other solutions
v2
public BasicEvent execute(Object[] args) throws Exception {
validate(url, folderId);
JSONObject jsonObject = doNetworkCall(url, title, folderId);
return new BasicEvent(jsonObject);
}
public void onSuccess(BasicEvent event) {
getListener().success();
}
public void onFailure(BasicEvent event) {
getListener().error(event.getException());
}
So… v2
Cache...
This is nicer...
But...
How it should be done?
v3?
public void execute(Event event) throws Exception {
try {
validate(event);
JSONObject jsonObject = doNetworkCall(event);
getListener().success(new BasicEvent(jsonObject));
} catch (Exception e) {
getListener().error(e);
}
}
v3?
// Cache should be here
public void execute(Event event) throws Exception {
try {
validate(event);
// Or inside here
JSONObject jsonObject = doNetworkCall(event);
getListener().success(new BasicEvent(jsonObject));
} catch (Exception e) {
getListener().error(e);
}
}
So… v3?
questions?
Questions?
tinkering with
android architectures
@nhpatt / Javier Gamarra
Future?