繁体   English   中英

Android Retrofit2 / RxJava2 / Room-简单数据处理

[英]Android Retrofit2/RxJava2/Room - Simple Data Processing

我正在处理应用程序,在应用程序启动时,我正在从Rest服务下载类别和帖子,以将它们存储在SQLite数据库中。 我有几个问题:

  1. 如何确定哪个对象是类别和哪个帖子? 或者我怎么能访问它们? objects变量很奇怪。
  2. 我应该将使用Room库在数据库中插入项目的代码放在哪里?
  3. 我需要为每个帖子下载图像,我应该在哪里下载?

码:

ItemsApi client = this.getClient(); // Retrofit2

List<Observable<?>> requests = new ArrayList<>();
requests.add(client.getCategories());
requests.add(client.getPosts());

Observable<Object> combined = Observable.zip(
        requests,
        new Function<Object[], Object>() {
            @Override
            public Object apply(Object[] objects) throws Exception {
                Timber.d("Length %s", objects.length); // Length 2
                Timber.d("objects.getClass() %s", objects.getClass()); // objects.getClass() class [Ljava.lang.Object;

                return new Object();
            }
        });

Disposable disposable = combined.subscribe(
        new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
               Timber.d("Object %s", o.toString());
            }
        },

        new Consumer<Throwable>() {
            @Override
            public void accept(Throwable e) throws Exception {
                Timber.d("error: %s", e.toString());
            }
        }
);


private ItemsApi getClient() {
    Retrofit.Builder builder = new Retrofit
            .Builder()
            .client(this.getOkHttpClient())
            .addConverterFactory(GsonConverterFactory.create(this.getGson()))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .baseUrl(Config.WEBSERVICE_URL_PREFIX);

    return builder.build().create(ItemsApi.class);
}

ItemsApi.class:

public interface ItemsApi {
    @GET("categories")
    Observable<List<CategoryEntity>> getCategories();

    @GET("posts")
    Observable<List<ArticleEntity>> getPosts();
}

这里是答案:

1)对于并行请求,应使用Observable.zip,如下所示

Observable<Boolean> obs = Observable.zip(
    client.getCategories(),
    client.getPosts(), 
    (categoriesList, postsList) -> {
         // you have here both categories and lists
         // write any code you like, for example inserting to db
         return true;
});

在这里,您具有每个类型的参数(categoriesList,postsList),即List和List。

2)您应该将代码放在我在注释中指定的位置。 确保您在正确的线程中

3)也可以在那里下载图像。 您可以在函数中使用另一个zip -s,将并行下载的图像,插入到db等组合在一起。所有这些都应该是可观察到的,并与zip组合在一起。

zip您可以根据需要组合任意数量的可观测对象,它们的结果将作为组合函数的参数提供。

1._您是否尝试改造的addConverterFactory

Retrofit restAdapter = new Retrofit.Builder().baseUrl("https://abc")
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        RestAuthenticationService restAuthenticationService = restAdapter.create(RestAuthenticationService.class);

RestAuthenticationService.class中

public interface RestAuthenticationService {

    @POST("security/login")
    Observable<User> login(@Body LoginRequest loginRequest);

}
  1. 您的意思是在本地处理缓存数据? 我认为您应该使用Realm而不是Room / native SQLite。

  2. 您应该使用PicassoGlide

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM