gpt4 book ai didi

java - 简化三重嵌套循环以避免 android Retrofit2 中的回调 hell (通过 RxJava2?)

转载 作者:行者123 更新时间:2023-12-02 10:19:19 24 4
gpt4 key购买 nike

上下文

使用android Retrofit2,需要访问深度嵌套name获取并显示其 Details 的字符串(其中 Detail s 对象引用了用于获取 GroupUserDetail 对象)。

JSONGroup 列表组成每个包含 User 的列表每个包含 name 的列表String这些模型中捕获的:

public class Group {
@SerializedName("id")
public String id;
@SerializedName("users")
public List<User> users;
}
public class User {
@SerializedName("id")
public String id;
@SerializedName("detailNames")
public List<String> detailNames;
}
public class Detail {
// allow access to objects used to get detail
public Group group;
public User user;
@SerializedName("name")
public String name;
@SerializedName("description")
public String description;
}

模型使用UserApi填充:

public interface UserApi {
@GET("groups")
Call<List<Group>> getGroups();

@GET("groups/{group_id}/users/{user_id}/details/{detail_name}")
Call<Detail> getDetail(
@Path("group_id") String groupId,
@Path("user_id") String userId,
@Path("detail_name") String detailName
);
}

目标

目的是使用给定的UserApi发出并解析显示 Dialog 的请求格式为:

Group1 (expandable heading)
User1 (expandable heading)
Detail1 (checkbox)
Detail2 (checkbox)
...
Group2 (expandable heading)
User2 (expandable heading)
Detail1 (checkbox)
...
...
...

问题

问题是当前解决请求Group s 并使用三重嵌套 for循环访问和获取Detail每个name s :

private void fetchDetails(List<Group> groupList) {
ArrayList<Group> groups = (ArrayList<Group>) groupList;
if (groups != null && groups.size() > 0) {
for (Group group : groups) {
for (User user: group.users) {
for (String detailName : user.detailNames) {
fetchDetail(group, user, detailName);
}
}
}
}
}

由于三重循环对每个 name 发出请求,问题变得更加严重。 ,并在getGroups内完成onResponse回调,这似乎不可读/不可维护:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUserApi = UserApiClient.getApi();
fetchGroups();
}
private void fetchGroups() {
Callback<List<Group>> groupsCall = new Callback<List<Group>>() {
@Override
public void onResponse(Call<List<Group>> call, Response<List<Group>> response) {
int statusCode = response.code();
switch (statusCode) {
case HttpURLConnection.HTTP_OK:
List<Group> groups = response.body();
fetchDetails(groups);
break;
}
}
@Override
public void onFailure(Call<List<Group>> call, Throwable t) {}
};
mUserApi.getGroups().enqueue(groupsCall);
}
private void fetchDetail(final Group group, final User user, String detailName) {
Callback<Detail> detailCallback= new Callback<Detail>() {
@Override
public void onResponse(Call<Detail> call, Response<Detail> response) {
int statusCode = response.code();
switch (statusCode) {
case HttpURLConnection.HTTP_OK:
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// display details in ListView
}
});
break;
}
}
@Override
public void onFailure(Call<Detail> call, Throwable t) {}
};
mUserApi.getDetail(group.id, user.id, detailName).enqueue(detailCallback);
}

建议使用 RxJava2 解决方案来避免像上述实现那样的嵌套回调,但由于管理 3 层嵌套以访问 names 方面存在困惑,该解决方案尚未完成。 :

Observable<List<Group>> groupCall =  mUserApi.getGroups();
groupCall.flatMapIterable(x -> x)
.flatMap(group -> {
Observable.fromIterable(group.users)
.flatMap(user -> {
Observable.fromIterable(user.detailNames)
.map(detailName -> {
mUserApi.getDetail(group.id, user.id, detailName)
.flatMap(detail -> {
detail.group = group;
detail.user = user;
return Observable.just(detail)
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Group>>() {
@Override
public void onSubscribe(Disposable d) {}
@Override
public void onNext(List<Detail> value) {
mDetails = (ArrayList<Detail>) value;
}
@Override
public void onError(Throwable e) {}
@Override
public void onComplete() {}
});
});
}
})

存在一些涉及 RxJava 中嵌套的问题(例如 RxJava multiple loop with condition ),但仍不确定如何将这些问题应用于深度嵌套的 name 。 s。

问题

是否可以使用RxJava2来避免回调 hell 并简化三元组for循环,是否有其他方法,或者解决方案应该诉诸 AsyncTask 内的同步请求/AsyncTaskLoader

最佳答案

正如我在评论中提到的,我认为您已经拥有的几乎是您可以获得的最简单的形式。但您似乎有兴趣在没有循环的情况下执行此操作,因此这里有一些建议(但不一定更好):

方法一:容器类

如果您愿意创建可以在单个对象中保存组、用户、详细名称的中间容器类,您可以执行以下操作:

首先,创建这些容器类:

public class UserWithGroup {
final Group group;
final User user;

public UserWithGroup(Group group, User user) {
this.group = group;
this.user = user;
}
}

public class DetailWithUser {
final Group group;
final User user;
final String detailName;

public DetailWithUser(Group group, User user, String detailName) {
this.group = group;
this.user = user;
this.detailName = detailName;
}
}

那么您使用 Java 8 Stream 的代码可以是:

private void fetchDetails(List<Group> groupList) {
groupList.stream()
.flatMap(g -> g.users.stream().map(u -> new UserWithGroup(g, u)))
.flatMap(ug -> ug.user.detailNames.stream().map(n -> new DetailWithUser(ug.group, ug.user, n)))
.forEach(d -> fetchDetail(d.group, d.user, d.detailName));
}

或者使用 RxJava:

private void fetchDetails2(List<Group> groupList) {
Observable.fromIterable(groupList)
.flatMap(g -> Observable.fromIterable(g.users).map(u -> new UserWithGroup(g, u)))
.flatMap(ug -> Observable.fromIterable(ug.user.detailNames).map(n -> new DetailWithUser(ug.group, ug.user, n)))
.flatMap(d -> mUserApi.getDetail(d.group.id, d.user.id, d.detailName)
.map(detail -> {
detail.group = d.group;
detail.user = d.user;
return detail
}))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(detail -> {
...
});
}

方法2:

Android.util.Pair 一个可以容纳任意两个对象的容器类。如果您使用它而不是创建中间容器,并且您对此感到满意,那么代码可以进一步简化。

Java 8 流和配对:

private void fetchDetails3(List<Group> groupList) {
groupList.stream()
.flatMap(g -> g.users.stream().map(u -> Pair.create(g, u)))
.flatMap(p -> p.second.detailNames.stream().map(n -> Pair.create(p, n)))
.forEach(p -> fetchDetail(p.first.first, p.first.second, p.second));
}

RxJava 和配对:

private void fetchDetails4(List<Group> groupList) {
Observable.fromIterable(groupList)
.flatMap(g -> Observable.fromIterable(g.users).map(u -> Pair.create(g, u)))
.flatMap(p -> Observable.fromIterable(p.second.detailNames).map(n -> Pair.create(p, n)))
.flatMap(p -> fetchDetail2(p.first.first, p.first.second, p.second)
.map(detail -> {
detail.group = d.group;
detail.user = d.user;
return detail
}))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(detail -> {
...
});
}

关于java - 简化三重嵌套循环以避免 android Retrofit2 中的回调 hell (通过 RxJava2?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54449201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com