如何通过推送通知通过NSNotification更新表格视图?
当我的应用通过App Delegate的- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
方法收到通知,并且该应用当前已在运行时,我会更新数据(通过Core Data存储)
弹出警报视图,一旦解除警报视图,我就会调用
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateConversations" object:nil];
在表视图控制器中:
- (void)viewDidLoad {
//some setup code removed
_someData = [[GGGroups findAllSortedBy:@"lastUpdated" ascending:NO withPredicate:[NSPredicate predicateWithFormat:@"ANY users = %@", [GGUser currentUser]]] mutableCopy];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData:) name:@"updateConversations" object:nil];
}
- (void)updateData:(NSNotification *)notification {
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
NSLog(@"Updated those conversations");
});
}
我尝试添加和删除dispatch_async主队列块。
它始终到达updateData方法(我可以看到NSLog),tableview本身永远不会更新。
我在这里做错了什么?
更新
根据要求提供更多代码。
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [_someData count];
}
此外,每个表视图单元都配置为_someData的索引。
appDelegate更新了GGGroups的核心数据结构,因此tableview应该相应地更新,但是这种情况不会发生,原因我到目前为止还不知道。
答案 0 :(得分:0)
您说您正在使用Core Data,但实际上您似乎并未观察到其中的更改(例如使用获取的结果控制器)。您正在设置_someData
,但在模型更改时不会更新它。
尝试在收到通知时更新用于填充表格视图的数据:
- (void)updateData:(NSNotification *)notification {
dispatch_async(dispatch_get_main_queue(), ^{
_someData = [[GGGroups findAllSortedBy:@"lastUpdated" ascending:NO withPredicate:[NSPredicate predicateWithFormat:@"ANY users = %@", [GGUser currentUser]]] mutableCopy];
[self.tableView reloadData];
NSLog(@"Updated those conversations");
});
}