显示 iOS 徽章编号/处理未读消息 (xCode - OBJECTIVE-C)
Displaying iOS badge number / Handling unread messages (xCode - OBJECTIVE-C)
我想更新收件箱徽章以显示未读消息的数量(我的消息不会自毁,应用类似于 iOS 消息)。
当用户点击一条消息并返回到收件箱选项卡时,应更新徽章。另外,我想将未读单元格的背景颜色标记为不同于已读单元格的背景颜色……这样用户就知道哪些已读,哪些未读。
我有一些工作代码,但现在我得到:
"Warning: A long-running operation is being executed on the main thread.
Break on warnBlockingOperationOnMainThread() to debug."
这是我用来更新未读消息的代码:
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
}
[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];
}
}
我想更新收件箱徽章以显示未读消息的数量(我的消息不会自毁,应用类似于 iOS 消息)。
当用户点击一条消息并返回到收件箱选项卡时,应更新徽章。另外,我想将未读单元格的背景颜色标记为不同于已读单元格的背景颜色……这样用户就知道哪些已读,哪些未读。
我有一些工作代码,但现在我得到:
"Warning: A long-running operation is being executed on the main thread. Break on warnBlockingOperationOnMainThread() to debug."
这是我用来更新未读消息的代码:
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
}
[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];
}
}