2

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?

klcjr89
  • 5,862
  • 10
  • 58
  • 91
Tom Testicool
  • 563
  • 7
  • 26
  • Wow that's also going to make your users unhappy when they use up their data plan on their phone. Why not let it be up to the user to initiate use of their data by using a `UIRefreshControl` instead? – klcjr89 Jul 26 '14 at 01:03
  • 1
    use push notifications. – Fosco Jul 26 '14 at 01:17
  • Can you explain what you mean by using push notifications? – Tom Testicool Jul 26 '14 at 03:10
  • Google push notifications, learn about them, implement them using Parse. When a notification comes, you could refresh your data source. Have a look at [Parse Chat](https://github.com/relatedcode/ParseChat) and look at the code, I suppose you could study how it updates its data. – duci9y Jul 26 '14 at 06:47

1 Answers1

2

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

PS70Y
  • 46
  • 1
  • I listened to you. And because of that I have opened an entire can of worms that I can't seem to put the lid on again. Can you please help me? I posted my question here. http://stackoverflow.com/questions/25045227/ios-receiving-remote-push-notifications-for-only-certain-view-controllers – Tom Testicool Jul 30 '14 at 19:51