Mark M. | has entered the room |
Mark M. | turned on guest access |
Apr 17 | 4:35 PM |
Eric | has entered the room |
Eric |
afternoon :)
|
Mark M. |
hello, Eric!
|
Mark M. |
how can I help you today?
|
Eric |
I am posting a List<Item> to my fragment from my viewmodel livedata in order to manually perform paging using notifyItemRangeInserted(offset, numItems). The problem I am facing is I don't want to use a variable offset in my fragment. is there a method I can use that provides an offset?
|
Mark M. |
well... that depends on what you're looking to get an offset from
|
Apr 17 | 4:40 PM |
Eric |
the way I currently implemented it, offset=0, 10, 20, basically adds pagesize every time
|
Eric |
actually List<Item>.size
|
Mark M. |
is the List<Item> being published only representing one page? or is it all loaded items, including a new page's worth?
|
Eric |
single page
|
Eric |
so publish 1=page 1, publish 2=page 2
|
Mark M. |
what are you doing when the user scrolls up?
|
Eric |
nothing, it works as is because I am adding to my list
|
Mark M. |
"I am adding to my list" does not seem to mesh with "single page" from my earlier question, though
|
Mark M. |
so, when you get a List<Item> from this LiveData, are you appending it to some master List<Item> that represents the entire data set (or the portion loaded to date)?
|
Eric |
I am appending it to a mster List<item> that represents the entire set of data, but I post per page
|
Mark M. |
OK, then if "actually List<Item>.size" refers to the size of the master List<Item>, off the cuff, that seems reasonable
|
Mark M. |
IOW, it should be whatever your Adapter is returning for getItemCount()
|
Apr 17 | 4:45 PM |
Mark M. |
which I assume is the size() of your master List<Item>
|
Eric |
yes, thanks
|
Eric |
is there a way for the viewmodel to know if my screen was rotated? I want to post my stored list in my viewmodel and return instead of calling into my model layer
|
Eric |
I could do this fine if I didn't paginate by seeing if my list was null but since I call it multiple times for a paginated list I can't
|
Mark M. |
well, IMHO, this feels like you're going about things incorrectly -- the viewmodel should be holding the master List<Item>, so it survives a configuration change
|
Mark M. |
blending in with the rest of your approach, the LiveData would publish the updated master List<Item> along with the page offset and size to pass to notifyItemRangeInserted()
|
Apr 17 | 4:50 PM |
Mark M. |
my assumption is that right now, your master List<Item> is just being held in your Adapter, which is why you're losing it on the configuration change
|
Mark M. |
FWIW, if you are doing this in Kotlin, the Jetpack Paging 3 library is rather nice and is much easier to use for custom data sources than was its predecessor
|
Eric |
actually, now that I think about it my live data is expecting a page of data, not a master list so I don't know if I can even differentiate between them in my observer
|
Eric |
I tried using the paging library with rxjava before and it was pretty difficult, not sure if it was improved
|
Mark M. |
Paging 3 + Kotlin + RxJava works well -- I'm using that for a customer's project right now
|
Apr 17 | 4:55 PM |
Eric |
View paste
|
Mark M. |
again, item 1 means that IMHO you are thinking about the problem incorrectly
|
Mark M. |
if you wish to hold onto certain data across a configuration change, the viewmodel should be owning that data
|
Mark M. |
if your master List<Item> is being owned by the Adapter, that's a problem
|
Mark M. |
so, for example, another way to handle your case is to use ListAdapter (the RecyclerView edition) and just call submitList() with each larger edition of your master List<Item>
|
Mark M. |
let it figure out which notify...() calls to make
|
Apr 17 | 5:00 PM |
Eric |
onCreateView will call my viewmodel to get data from my model. My viewmodel does contain my master list, so I'm not sure I understand y9ou
|
Mark M. |
"My viewmodel does contain my master list" -- not according to other entriese that you posted in the chat, such as "I want to post my stored list in my viewmodel", though it is possible that I am getting lost in your various lists
|
Eric |
I meant my viewmodel posts pages but also keeps track of a master list. sorry it was confusing
|
Mark M. |
then item #1 should already be solved ("my viewmodel knowing my screen was rotated to avoid fetching from my model")
|
Mark M. |
if your problem is that your fragment is doing something in onViewCreated() (or wherever) that is telling your viewmodel to fetch from your model, that is your problem
|
Eric |
but in onCreateView I call viewModel.getData()
|
Mark M. |
then getData() needs to be smart enough to look at the master List<Item> and say "hey, I already have data; I do not need to fetch it again"
|
Mark M. |
or something like that
|
Mark M. |
alternatively, get rid of getData() and have your viewmodel load the data from the model in the constructor/init block
|
Apr 17 | 5:05 PM |
Eric |
yes, that was my reaso for asking if my viewmodel knew if a rotation just occured
|
Apr 17 | 5:05 PM |
Mark M. |
anyway, I am out of time for today's chat -- the transcript will be posted to https://commonsware.com/office-hours/ shortly
|
Mark M. |
the next chat is Tuesday in the 8:30am US Eastern time slot
|
Eric |
ok I appreiate it
|
Mark M. |
have a pleasant day!
|
Eric | has left the room |
Mark M. | turned off guest access |