Martin Erlic
Martin Erlic

Reputation: 5667

Increment list with Parse LiveQuery (Android)?

I'm trying to display the number of new objects that appear in my database in real-time using this library: https://github.com/parse-community/ParseLiveQuery-Android.

I'm listening for new objects that appear in my Notifications class. How would I increment that value in my StringHolder below 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() + ""));
 }
});

Upvotes: 0

Views: 283

Answers (1)

Arturo Jamaica
Arturo Jamaica

Reputation: 73

I think you are declaring the newNotifications variable each time a onEvent is made. Try putting this line in a shared context like and activity or singleton :

List<ParseObject> newNotifications = new ArrayList<>();

Upvotes: 1

Related Questions