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?
Use Push notifications.
When a user sends a message to another user, you could get the app to send the recepient a push notification. You can choose whether to display a banner or not, or simply update the badge.
Guide is located here - Parse Push guide
Parse push is also free to a certain amount of notifications, but a much better way than polling the DB every x amount of seconds