Scenario = I have an app that allows users to message other users. I also have a button that will display that amount of new (unread) messages that the user has. The amount will be displayed on a badge icon on the tab bar at the bottom.
What I've Been Doing = This queries every 5 seconds if a new message has been posted for the current user.
- (void)queryForMessagesBadgeNumber:(NSTimer *)timer
{
NSString *currentUserID = [PFUser currentUser][@"userID"];
PFQuery *badgeQuery = [PFQuery queryWithClassName:@"Message"];
[badgeQuery whereKey:@"receiverID" equalTo:currentUserID];
[badgeQuery whereKey:@"wasRead" equalTo:[NSNumber numberWithBool:NO]];
[badgeQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error)
{
if (number == 0)
{
NSLog(@"No unread messages");
}
else
{
[[self.tabBar.items objectAtIndex:2] setBadgeValue:[NSString stringWithFormat:@"%d",number]];
}
}];
}
Issues = This is very taxing on the "Requests Per Second" count. Parse allows for 30req/sec on freemium and just with my one phone I am using half of that with this query.
Question = Does anyone know of a more efficient way I can achieve what I am doing here?