小编Ste*_*eve的帖子

带RxJava2的Android Room中的交易

我的应用程序的要求是允许用户执行多个步骤,然后在完成后根据每个步骤中的条目将值写入数据库。UI中的每个步骤都可能有助于将需要写入数据库的操作。数据可能在多个表中,并且与这些表中的不同行有关。如果任何数据库操作失败,则整个操作应失败。

我最初考虑将所有数据加载到内存中,进行操作,然后在每个可能的实体中调用update方法(使用REPLACE的冲突策略),但是内存中可能有非常大量的数据。

我认为我可以组装一个List,其中显示中的每个Fragment都贡献一个或多个Completable,然后在UI流结束时使用Completable.concat()顺序执行这些。如下所示:

    Completable one = Completable.fromAction(() -> Log.w(LOG_TAG, "(1)")).delay(1, TimeUnit.SECONDS);
    Completable two = Completable.fromAction(() -> Log.w(LOG_TAG, "(2)")).delay(2, TimeUnit.SECONDS);
    Completable three = Completable.fromAction(() -> Log.w(LOG_TAG, "(3)")).delay(3, TimeUnit.SECONDS);
    Completable four = Completable.fromAction(() -> Log.w(LOG_TAG, "(4)")).delay(3, TimeUnit.SECONDS);

    Completable.concatArray(one, two, three, four)
            .doOnSubscribe(__ -> {
                mRoomDatabase.beginTransaction();
            })
            .doOnComplete(() -> {
                mRoomDatabase.setTransactionSuccessful();
            })
            .doFinally(() -> {
                mRoomDatabase.endTransaction();
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe();
Run Code Online (Sandbox Code Playgroud)

Completables实际上是Room DAO插入/更新/删除方法的包装。我可能还会在完成时执行UI操作,这就是为什么我在主线程上进行观察的原因。

当我执行此代码时,我得到以下日志:

W/MyPresenter: Begin transaction.
W/MyPresenter: (1)
W/MyPresenter: (2)
W/MyPresenter: (3)
W/MyPresenter: (4)
W/MyPresenter: Set transaction successful.
W/MyPresenter: End transaction. …
Run Code Online (Sandbox Code Playgroud)

android rx-java rx-java2 android-room

5
推荐指数
1
解决办法
1730
查看次数

如何使用GSON将空字符串视为空对象?

我正在从Reddit API中检索评论.该模型具有线程,以便每个注释可以在内部具有注释列表,命名为回复.以下是JSON响应的外观示例:

[
   {
      "kind":"Listing",
      "data":{
         "children":[
            {
               "data":{
                  "body":"comment",
                  "replies":{
                     "kind":"Listing",
                     "data":{
                        "children":[
                           {
                              "data":{
                                 "body":"reply to comment",
                                 "replies":""
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
]
Run Code Online (Sandbox Code Playgroud)

以下是我使用POJO进行建模的方法.上述响应将被视为CommentListings列表.

public class CommentListing {
    @SerializedName("data")
    private CommentListingData data;
}

public final class CommentListingData {
    @SerializedName("children")
    private List<Comment> comments;
}

public class Comment {
    @SerializedName("data")
    private CommentData data;
}

public class CommentData {
    @SerializedName("body")
    private String body;

    @SerializedName("replies")
    private CommentListing replies;
}
Run Code Online (Sandbox Code Playgroud)

注意底层CommentData POJO如何引用另一个名为"replies"的CommentList.

此模型有效,直到GSON到达最后一个没有回复的子CommentData.API不提供null,而是提供空字符串.当然,这会导致GSON异常,它需要一个对象但却找到一个String:

"replies":""
Run Code Online (Sandbox Code Playgroud)

预计BEGIN_OBJECT但是STRING …

java json reddit gson

3
推荐指数
1
解决办法
2366
查看次数

标签 统计

android ×1

android-room ×1

gson ×1

java ×1

json ×1

reddit ×1

rx-java ×1

rx-java2 ×1