2015-09-10 64 views
0

我想更新收件箱徽章以显示未读邮件的数量(我的邮件不会自毁,应用类似于iOS邮件)。显示iOS徽章号码/处理未读邮件(xCode - OBJECTIVE-C)

当用户点击邮件并返回到收件箱标签时,徽章应该被更新。此外,我想标记未读单元格的背景颜色与读取单元格的方式不同......用户知道已读和未读的内容。

我有一些工作的代码,但现在我得到:

“警告:正在主线程上执行的长期运行的操作上warnBlockingOperationOnMainThread()调试 休息。”

这是我的代码有更新未读邮件:

PFQuery *query = [PFQuery queryWithClassName:@"Messages"]; 
    [query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]]; 
    [query orderByDescending:@"createdAt"]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (error) { 
      NSLog(@"Error: %@ %@", error, [error userInfo]); 
     } else { 
      // messages were found! 
      self.messages = objects; 

      //find unread messages 
      [query whereKey:@"readBy" notEqualTo:[[PFUser currentUser] objectId]]; 
      self.unreadMessages = [query findObjects]; 

      // set badge # to number of msgs 
      if (self.unreadMessages.count > 0) { 

       [[self navigationController] tabBarItem].badgeValue = [NSString stringWithFormat:@"%lu", (unsigned long)self.unreadMessages.count]; 
      } 

      [self.tableView reloadData]; 

代码更新小区:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

PFObject *message = [self.messages objectAtIndex:indexPath.row]; 
cell.textLabel.text = [message objectForKey:@"senderEmail"]; 

if ([self.unreadMessages containsObject:message]) { 
    // color background gray, bold the font 
}else{ 
    // leave cell alone 
} 
+0

你可以在[查询findObjects此警告]行,使用方法findObjectsInBackgroundWithBlock()而不是findObjects。 –

+0

我将如何设置该行代码?另外,我希望未读消息加载所有消息的同一时间加载。我已经使用了两个“findobjectsInBackWithBlock”代码,并且只有一个运行,直到我刷新了表视图 –

回答

0
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) 
{ 
    if (!error) 
    { 
     self.unreadMessages = [NSMutableArray arrayWithArray:objects]; 
     // set badge # to number of msgs 
     if (self.unreadMessages.count > 0) 
     { 
      [[self navigationController] tabBarItem].badgeValue = [NSString stringWithFormat:@"%lu", (unsigned long)self.unreadMessages.count]; 
     } 
     [self.tableView reloadData]; 
    } 
} 
+0

这似乎工作!非常感谢你 –

+0

刚刚添加我的代码更新单元根据读取或未读。我将不胜感激任何帮助!现在self.unreadmessages从不包含消息 –