我有一个管理器,它封装了对改造服务的调用,因为我需要在登录时保存会话令牌和对象,然后:
class LoginManager {
public static Observable<Session> login(String email, String password) {
Credential credential = new Credential(email, password);
Observable<Session> observable = SERVICE.createSession(credential);
observable.subscribe(new Subscriber<Session>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Session session) {
String token = session.getToken();
saveToken(token);
save(session.buildCustomer());
}
});
return observable;
}
}
我的服务看起来像:
public interface CustomerService {
@POST("/api/sessions/customer")
Observable<Session> createSession(@Body Credential credential);
}
在我的活动中使用经理内部和外部的可观察对象是否正确?
它可以有什么问题?
- - 编辑
改为地图:
class LoginManager {
public static Observable<Session> login(String email, String password) {
Credential credential = new Credential(email, password);
return SERVICE.createSession(credential)
.map(session - {
String token = session.getToken();
saveToken(token);
save(session.buildCustomer());
return session;
});
}
}
这是个好主意吗?