android retrofit数据库,rx java - Android and RxJava: insert into Room database a Retrofit Response - St...

I have two slightly different Question classes. One is an retrofit call results object, and the other is a Room @Entity in my Android App.

And now I want from my Interactor class (Use-case) class do the following:

Make a call to the API and result (List where question is

the Retrofit response class)

On success, make a new Game object in my Room database. This operation have long (@Entity id which is autogenerated) as return

type.

for each Question from retrofit response (from (1)), question -> Converter which converts from retrofit.Question to

database.Question. Converter method takes 2 parameters, the

retrofit.Question object and the ID which was returned in step (2).

After conversion, add to database.

Observe on AndroidSchedulers.mainthread. (subscribeOn is called from repository)

Now the problem I am having is creating this stream with RxJava from my Interactor class.

Here is all the classes and calls. First is my Interactor.class method which should do the stream described above:

public Single> getQuestionByCategoryMultiple(String parameter);

The API CALL from MyAPI.class:

//this Question is of database.Question.

Single> getQuestionByCategory(String parameter);

The Room database repository.class:

Single addGameReturnId(Game game);

Completable addQuestions(List questions);

Converter.class:

public static List toDatabase(List toConvert, int id);

I am having trouble creating the stream described above with these methods. I tried a mix of .flatmap, .zip, .doOnSuccess, etc without successfully creating the stream.

If there is anything else you need me to explain, or explain the problem better, please comment below.

public Single> getQuestionByCategoryMultiple(String parameters){

return openTDBType

.getQuestionByCategory(paramters) //step 1

// step 2

// step 3

.observeOn(AndroidSchedulers.mainThread()); //step 4

}

EDIT:

I tried something like this:

return openTDBType

.getQuestionByCategory(parameters)

.map(QuestionConverter::toDatabase)

.flatMap(questions -> {

int id = gameRepositoryType.addGameReturnId(new Game(parameters).blockingGet().intValue();

questions.forEach(question -> question.setqId(id));

gameRepositoryType.addQuestions(questions);

return gameRepositoryType.getAllQuestions(); })

.observeOn(AndroidSchedulers.mainThread());

^^ I don't know if this is the best way to go about this one? Can anyone confirm if this is a good way to design what I want to do here, or if there are better ways or any suggestions?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值