Incrementing results of LiveQuery?
SeloSlav opened this issue · 1 comments
Hey, cool repo! I've been excited to try LiveQuery for Android for some time now. Thanks for building this up.
I'm trying to display the number of "unread" new notifications in my MaterialDrawer. I'm listening for new objects in my Notifications class. How would I increment that value in my StringHolder each time a new Notification is created? Whether or not the UI is updated is another story I suppose. But can I at least increment that list in real time?
ParseLiveQueryClient parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient();
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery(ParseConstants.CLASS_NOTIFICATIONS);
parseQuery.whereEqualTo(ParseConstants.KEY_READ_STATE, false);
SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery);
subscriptionHandling.handleEvents(new SubscriptionHandling.HandleEventsCallback<ParseObject>() {
@Override
public void onEvents(ParseQuery<ParseObject> query, SubscriptionHandling.Event event, ParseObject object) {
// HANDLING all events
List<ParseObject> newNotifications = new ArrayList<>();
newNotifications.add(object);
Log.w(getLocalClassName(), "New notification found! " + object.toString());
result.updateBadge(5, new StringHolder(newNotifications.size() + ""));
}
});
Hello,
You are currently adding the result of the query in a new list, so the count will always be 1.
Actually, LiveQuery do not support retrieve the count of the query. I do not even think it's in the plan to support that kind of feature. But right now, I will suggest 2 possibles solutions to fix your problem:
1st solution:
First, execute a normal parse query (findInBackground) returning the current List<> of your notification and store it locally. Then subscribe with liveQuery, and for each CREATE/ENTER or LEAVE/DELETE, update your local list. Note: You could only keep the list of IDs in a SET (optimal).
Pros: Only affect the client, nothing to do on the backend side.
Cons: Need to store locally the list and make sure to update it properly. Also, you will transfer the whole object for every alteration when you only need a count of the total.
2nd solution:
Create new table or column (in your user model) representing only the notification count. Subscribe to that table (model). And update your badge with that count.
To update that count in your model, you will need to create an "afterSave" and "afterDelete" functions on Parse Server, that will count and update unread notifications.
Pros: Only transfer the data you need, it's lightweight. Do not need to store locally the list of notification.
Cons: You need to alter the schema and code you afterSave an afterDelete.
I hope it's clear, otherwise, just ask for more details.
Thanks.