I am new to Rx. I have to POST total Y number of i...
# rx
s
I am new to Rx. I have to POST total Y number of items in a batches of X items at a time via retrofit. Which Observable, operator, etc. should I be using? I want Rx to send X items at a time until total Y number of items have been sent.
g
Retrofit? But retrofit always emits 1 item (because only 1 response per request), so instead of Observable better to use Completable/Single But I’m still not sure what exactly do you want to achive, convert a list of items to Observable which emits each item separately?
g
can you use
.buffer(X)
?
g
for what? %) Retrofit never emits more than 1 element. Would be good to understand what Sudhir tries to achive
g
you don't buffer the Retrofit response, you buffer what needs to be sent. For example:
listOf(1,2,3,4,5,6).toObservable().buffer(3).flatMap {retrofitService.send3Numbers()}
g
I see, yeah bufffer looks good for this
s
I have Y number of items in a Realm database which I have to send 50 items at a time via a Retrofit POST call. I am wondering how to achieve this using Rx.
g
Request 50 items and send?
s
No, I have all the items saved locally in a Realm Database. I am creating a Map object of 50 records from Realm Database and POST-ing this via Retrofit. Now sending all items via a single Retrofit call is easy but it times out. I want to use Rx and send 50 items saved in a single Map object and do N number of Retrofit calls. So if I have 340 items then I would make 7 retrofit call.
g
What exactly do you get from Realm? Full list of all records?
s
Currently there's only one network call that I have to do I get all results from Realm into a RealmResults<SomeDataClass>. Then I add all of these items to the Map in a key and a value pair. This is provided to the Retrofit instance which POSTs it.
g
Get all records and split them, you can use
chunked
extension fucntion to split one list to many list with particular amount of items, then send each item to retrofit and merge results
You can use something like that:
Copy code
Observable.just(1,2,3,4,5) // this is list of your records
   .flatMapIterable { it.chunked(3) } // split one list to many lists with particular amount of items
   .flatMap { networkCall(it) } // send each item
   .subscribe { println(it) } // print results, you also probably want handle errors
Approach with buffer will also work, but you should emit each record separately (you also can use flatMapIterable for that
u
yea what they said, chunk it into lists of size that is passsable, and the just "for loop" the chunks
s
Thank you for your help. I need to spend some time understanding Rx. I ended up using Recursion for this particular issue.
g
Recursion? Hmm, little bit strange solution for this case