Parse query from createdAt for android - android

In my android project i am working on adding a pull to refresh, Since there is nothing mentioned in the document,where i can add pull to refresh directly i have used android SwipeRefreshLayout and developed a pull to refresh functionality. For that to success i need to get latest data set onRefresh() method. So i have saved the last shown ParseObject "createdAt" time as getLastPostDate() and tried to query latest data from last time up to now every time user pulls to refresh as like below query.
query.whereGreaterThanOrEqualTo("createdAt", getLastPostDate());
postList = query.find();
But for some reason i can't query from "createdAt" or "updatedAt" value. Help is appreciated.
EDIT
#JayDev here how i did it.
final ParseQuery<Post> query = Post.getQuery();
query.orderByDescending("createdAt");
postList = query.find(); // getting all the posts
setLastPostDate(postList.get(0).getCreatedAt()); // set the last post date
since posts are in descending order , get i can get the last post from 0 element. (This works!!)
on refresh i use the above code, to get latest posts posted after that date. Please guide me if there is another way to

Related

android - Grabbing collectionGroups in a query returning empty

I'm working on using a collectionGroup query and trying to pass it to a Recyclerview but it seems to always be coming up empty.
Firstly, my understanding is that collectionGroups used not to be so great but now I hear it's fixed and it allows a query to grab all documents under a collectionGroup name.
The query I show below doesn't grab anything and I can't figure out why. From the explanation in the documentation, it should be able to grab all bookPendingRequests items across all books(the parent Items) that have an owner of whoever is logged in.
String mAuthUser = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
the line above allows me to grab the logged in username
Query query = firestoreDB.collectionGroup("bookPendingRequests").whereEqualTo("mOwner", mAuthUser)
This line above is what i imagine should work.
code
FirestoreRecyclerOptions<BookRequestModel> options = new FirestoreRecyclerOptions.Builder<BookRequestModel>()
.setQuery(query, BookRequestModel.class)
.build();
mBookRequestsAdapter = new BookRequestsAdapter(options);
RecyclerView bookRequestListRecyclerView = findViewById(R.id.bookRequestListRecyclerViewId);
bookRequestListRecyclerView.setHasFixedSize(true);
bookRequestListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
bookRequestListRecyclerView.setAdapter(mBookRequestsAdapter);
here's an image of what it looks link in firebase:
here's an image of my index for the collectionGroup
Right now I'm just trying to get the query to actually grab the "bookPendingRequests".
Any help or guidance is appreciated.

How to notify user that new data is avalable so that they need to refresh?

I'm working on a news app. I fetch data from server directly in JSON format, parse it and show in views. I would like to be able to be able to notfify the use that new data is avaible and they need to refresh instead of automatically updating.
How can I achieve this ?
The simplest way would be to compare the json data string value to the new value from the server. Something like this:
if (currentJSONDataString != newJSONDataString) {
Toast.make(context, “Updates available. Please refresh”, Toast.LENGTH_SHORT).show()
}
The limitation here though is that this only checks to see if your current data differs in any way from the server data. If you had things in your data model such as a datetime field that indicated the last updated at time or something similar you could do a more accurate comparison to check if the current data is before the newer data.

Cloud Function to sort the Firebase Child from inner child

I need to have one problem sorted out. I want a cloud function, where I need to loop through every Posts that has uniqueID and I want to sort those uniqueID's according to the value of TotalReactions. To me more vivid, suppose, the first post has totalReactions = 5, and second one has totalReactions = 6, then I need to have another RealTimeDatabase "PostArranged" which sorts them in descending order, so second post which has Total Reaction would be in first and then follows the one which has lesser TotalReaction...
Here is how my firebase realtime database looks like:
Posts
-L29TD-nsUYRu3wYcCQl
Caption: "First Screenshot"
CurrentUserReaction: "notreacted"
Image: "https://firebasestorage.googleapis.com/v0/b/nep..."
ReactingUser
<user-id>:"1"
<user-id>:"2"
Time: "13:40:54"
TotalReactions: "2"
Unique: "-L29TD-nsUYRu3wYcCQl"
UserPhoto: "https://firebasestorage.googleapis.com/v0/b/nep..."
Username: "Me"
Any help would be appreciated ..
This is not a good way to do what you want to do. In this method the database has to be sorted to another "ArrangedDatabase" every time a reaction is added which will happen in a high frequency. So it will be very expensive to do this with Cloud Functions.
Instead use orderByChild(), orderByKey(), orderByValue() to sort data when you're querying data.
Sorting data - Firebase documentation this show it quite well.
For anyone who want to do this in your RecyclerViewAdapter provided by Firebase, it is easily done... When you pass in those 4 parameters. Do not pass the database Reference, instead, make a new query, and make sure to sort that thing either by OrderByChild(), OrderByValue() or anything you want to order by... i.e.
In my case, I will do something like this:
q = mDatabaseReference.orderByChild("TotalReactions");
and pass q as parameter inside FirebaseAdapter, like this:
FirebaseRecyclerAdapter<Posts,PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostViewHolder>(
Posts.class, R.layout.each_post_layout , PostViewHolder.class, q
) {
.
}

Firebase cannot get the last insert value

I have a small firebase database with a list. How can I get the last inserted value of the list?
The elements of the list have as key a date string, example:
2016_05_29 --> data
2016_05_30 --> data
2016_05_31
2016_06_01
2016_06_02
2016_06_03
2016_06_10
2016_06_11
2016_06_12
2016_06_13
2016_06_14
2016_06_15
2016_06_16
2016_06_17
2016_06_18
2016_06_19 //returned value
2016_06_20
2016_06_21
2016_06_22
2016_06_23 //expected value
I am try to get the last value of this list, so 2016_06_23 but for some reason it returns this 2016_06_19 whatever key/order system I use:
Query dateMaxRef = ref.orderByKey().limitToLast(1); //this returns always 2016_06_19 instead of 2016_06_23
dateMaxRef.addListenerForSingleValueEvent(new ValueEventListener()
....
Any idea?
I faced the same issue and for me the reason was because the query was not using the latest data from firebase and instead relying on internal cache. Using .keepSynced(true) on the query fixed it for me. Below is the sample code -
Query causeListPath = mFirebase.child(PATH_CAUSE_LIST).child(SELECTED_CITY)
.child(SELECTED_COURT_CODE).limitToLast(1);
// If user wants un-cached data, force sync
if (!useCache) causeListPath.keepSynced(true);
causeListPath.addChildEventListener(new ChildEventListener() {
...
});

Realm Query Android

I'm getting started in using Realm in my android project..
everything was going well.. But I was trying to filter data but I couldn't.
I want to make a query like this:
SELECT * FROM messages WHERE (fromId='id1' AND toId='id2') OR (fromId='id2' AND toId='id1');
Here's my code :
RealmResults<com.intcore.android.tawasol.Message> results =
realm.where(com.intcore.android.tawasol.Message.class).beginGroup().equalTo("fromId", fromId).equalTo("toId", toId).endGroup().or().
beginGroup().equalTo("fromId", toId).equalTo("toId", fromId).endGroup().findAllSorted("date", true);
but this returns only 1 result.. it should return 2 results.
Any Idea?
Your query looks fine. Try split the query into result and then sort it.
RealmResults<com.intcore.android.tawasol.Message> results;
results = realm.where(com.intcore.android.tawasol.Message.class).
beginGroup().equalTo("fromId", fromId).equalTo("toId", toId).endGroup()
.or().
beginGroup().equalTo("fromId", toId).equalTo("toId", fromId).endGroup()
.findAll();
results.sort("date") // ascending
results.sort("date", RealmResults.SORT_ORDER_DESCENDING); // descending

Categories

Resources