- map
- flatmap
- Case: Getting User Information after Logging in
map operator
Map applies a function of your choice to each data transmitted by the original Observable, and then returns an Observable that emits the results. This sentence summarizes the role of map, and then does not understand the meaning of this sentence. Understand it through the following code
private void testMap() {
//Create Observable
Observable.just(1).map(new Function<Integer, String>() {
//Function < Integer, String >, the first parameter is the original data, and the last parameter is the type that needs to be converted.
@Override
public String apply(@NonNull Integer integer) throws Exception {
//do something
return 1+ "int Become String";
}
}).subscribe(new Observer<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull String s) {
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
If there are no map operation symbols, the data transmitted by Observable is shaping and Observer is shaping, but through the map operator, the shaping can be transformed into String type, Observer is also Observer type, map operation can convert the original data type into any new type you need, data object, can be url address, only need the map operator to apply. The method is processed to return the converted data type, and the original Observable type is changed.
flatmap operator
flatmap converts an Observable that emits data into multiple Observables, then merges the data they emit into a single Observable, which you can understand by code.
private void testFlatMap() {
Log.e(TAG, "testFlatMap: " );
//Converting shaping data to String type, converting String type to User object
Observable.just(1).flatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(@NonNull Integer integer) throws Exception {
//Converting Shapes to String Types
Log.e(TAG, "apply: "+integer );
String s = integer + "flatmap";
Log.e(TAG, "apply:s "+s );
return Observable.just(s);
}
}).flatMap(new Function<String, ObservableSource<User>>() {
@Override
public ObservableSource<User> apply(@NonNull String s) throws Exception {
//Initiate network requests through s, simulate
Log.e(TAG, "apply: "+s+"Acquired User Object" );
User user = new User("Account number", "Password");
return Observable.just(user);
}
}).subscribe(new Observer<User>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull User user) {
Log.e(TAG, "onNext: "+user.getPassword()+user.getUsername() );
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
Obtain user information after login
Business logic:
1 Get username and password
2 Log in with username and password (make network requests)
3. The network request is successful. The server sends the user's information and the client receives it.
flatmap's apply return value is Observable Source, which emits the converted data.
private void login() {
Observable.just(getUserParam()).
flatMap(new Function<String, ObservableSource<BaseResult>>() {
@Override
public ObservableSource<BaseResult> apply(@NonNull String s) throws Exception {
Log.e(TAG, "apply: "+s );
//Make a network request to get BaseResult
BaseResult baseResult = new BaseResult("user_id" );
return Observable.just(baseResult);
}
}).flatMap(new Function<BaseResult, ObservableSource<User>>() {
@Override
public ObservableSource<User> apply(@NonNull BaseResult baseResult) throws Exception {
//Make network requests to get User objects
User user = new User("Army", "123456");
return Observable.just(user);
}
}).subscribe(new Observer<User>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull User user) {
Log.e(TAG, "onNext: "+user.getUsername() );
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
//Get the username and password
private String getUserParam() {
return mEtName.getText().toString() + mEtPassword.getText().toString();
}