2015-09-10 60 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從不包含消息 –