MVVM : fetch data from server and notify item - android

I will take an example to explain my problem : in a search screen, I need to search for a big list of movies.
I can bookmark each movie, so I need to notify my item in my recycler, here is my problem.
Do I need to put every movies searched in room database and it will be simple after for the notify with MVVM & DiffUtils?
Because, when I used MVP, I created a small database with MovieId and a boolean for bookmark, and when the user bookmark a movie, I run through in my items in adapter and notify the concerned item. But this solution is very ugly now with MVVM.
Thanks in advance

Just for the search screen you don't need to save data to local database. Instead of that you can just bind one list to RecyclerView and update it with another list from API using DiffUtils.

Related

How to submit non pagingData(just list of objects) to PagingDataAdapter?

I just implemented pagination with paging 3 library. So, I did everything according to documentation.
From backend I query a list of Posts, and it comes paginated with next and prev keys, that's why I decided to use paging 3. Then, after converting it PagindData in viewModel, I submitted it to adapter using submitData.
Ok, but I have i spinner which shows a category list, and I want to display posts by category. Posts by category also comes from backend, but it is not paginated it comes just as a list of objects. And question is how to pass this list of posts by category to my recycler view which works with PagingData? Should I convert posts by category to pagingData ? Thanks.
To send a static list you can use PagingData.from(list), but if your list isn't paginated, Paging might just be unnecessary complexity for that part of your app!

How to update list item when using paging library and custom datasource?

I am using paging library and everything is working good when I am directly returning DataSource.Factory<Int, Data> from Dao and then setting my paged list like this
pagedList = LivePagedListBuilder(dao().getAllData(), config).build()
When I click on my list item, it changes, updates the room database and with paging automatically updates the UI. But I need to use a custom data source. So I created one and now I am setting my paged list like this
pagedList = LivePagedListBuilder(customDatasourceFactory, config).build().
The problem is that now when I update a row in my room table, the UI is not automatically updated. I am also using invalidate() when my dataset is changed. But in this case when I want to update only a single item, I can not use it, because my positional datasource then loads the first page and when I am in the middle of my list, alter list item, it jumps to the top.
So basically what I want is to implement the same behaviour that I had when I was returning directly Datasource Factory from Dao, but now with my own datasource.

How can I load more data on scrolling down with FirebaseIndexRecyclerAdapter

I have data that was insert PostKey and Rating by cloud function. Following this picture newfeed, post
and I use FirebaseIndexRecyclerAdapter to get data
FirebaseIndexRecyclerAdapter<Post, FeedFragment.PostViewHolder> firebaseIndexRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Post, FeedFragment.PostViewHolder>(
Post.class,
R.layout.post_row,
FeedFragment.PostViewHolder.class,
FirebaseRef.mNewfeedRef.child(UID),
FirebaseRef.mPostRef
) {
Solution that I was thinks is
First one is orderByValue() and load StartAt(0) to EndAt(5) when scrolled to bottom it called StartAt(0) to EndAt(10) and so on but each time will load old data that could wasted traffic.
Second one is create new reference suppose is "newfeed-post" that have only top 5 items ordered by rating that get from "newfeed" and when I scroll down to load more I request to cloud function to let cloud function insert more 5 items from "newfeed" to "newfeed-post" that lower rating (eg. position 6-10).
Please help and let me know how to do this. Thanks :D
Pagination is not supported by FirebaseUI-Android components. They currently require you to provide a single query that yields all the results to display. There is no way to progressively supply more queries as a list is scrolled. Also see the discussion in this issue on the Github repo for FirebaseUI-Android.

How to show data from database into a RecyclerView with high performance?

I wrote my own CursorAdapter for RecyclerView like following link: https://gist.github.com/skyfishjy/443b7448f59be978bc59
Then I found whenever I change something in database and want to show it in RecyclerView, I need to create a new Cursor by db.query() and use CursorAdpater's changeCursor(). Since query() will scan all rows in database, the RecyclerView will refresh slowly when data amount is big even I insert only one row into database.
Besides, as we all know, RecyclerView provides notifyItemInserted/Removed(position) for developers so that the RecyclerView can refresh partly, which is useful and beneficial to memory/time. However, when I use CursorAdapter, I don't know when and how I can use these methods because changing cursor isn't adding something directly to dataset binding with RecyclerView but refreshing all items in fact.
So are there any better ways to show data from database in RecyclerView and use RecyclerView's improving method to show variety of database?
I can tell you what i've done...
A. Loaded a cursor using Loader.
B. Copied the cursor into arraylist that is attached to the adapter (the cursor isnt attached to the adapter directly), close the cursor. Works well if there isnt a lot of data - if there is a lot rows then i would have load some of it to the arraylist and then when the user would scroll down i would query again and load from the last row of the array.
C. When the user would like to delete or add something i would do the operation on arrayList first (UI thread) notifiyItemChanged and then change the db (Back thread)
Hope i helped.

How to update ListView on scrolling while retrieving data from server in Android?

Currently, I'm using AsyncTask to handle Http connection and retrieve data as JSON format.
Loading all data is trivial but it consumes too much time, so I decided to switch to load 10 items at a time using LIMIT OFFSET (mysql).
Next I set up the event onScroll for my list view to create a new AsyncTask each time user scroll. However, from what I read, AsyncTask is stored in a thread pool which is limited 5 threads at a time, so I'm not sure this is a right approach. I'm newbie to client/server app, so could I anyone give me an advice on this issue? Any related article, documentation would be greatly appreciated.
Here are few useful links for it,
Android: Implementing progressbar and "loading..." for Endless List like Android Market
Endless Listview with current Async Task
Android Endless List
http://www.androidguys.com/2009/10/21/tutorial-autogrowing-listview/
http://mylifewithandroid.blogspot.com/2010/03/progressively-loading-listviews.html
In simple steps,
As user scrolls – detect the end of the list 1)Display a progress
notification 2)Ask for update 3)Receive update (asynchronously) and
extend list
A typical approach would be e.g. to load 25 initially and then have a footer in the list that displays e.g. the current count and the total count and upon pressing loads another 25 and so on. That would be a paged sort of loading.
When you do that you have to keep the current position and notify the adapter that the list has changed.
If you are using a ListView, I believe I can safely assume that you must be using some sort of ListAdapter. Instead of starting a new AsyncTask in the onScroll event, you should maintain just one single AsyncTask to retrieve data from the server, add that data to the ListAdapter dataset and then call notifyDatasetChanged on the ListAdapter.
The ListAdapter and ListView will take care of the rest.

Categories

Resources