2013-05-20 94 views
1

如何通过推送通知通过NSNotification获取表视图?通过通知更新Tableview

当我的应用程序收到通知时,通过应用程序委托的- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo方法和应用,目前已经在运行,我更新数据(通过核心数据存储)

查看警报弹出,一旦被驳回,我请

[[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应该相应地更新,但这不会发生,因为我迄今为止未知的原因。

+1

NSLog的发生,对不对? – Undo

+3

你确定你在IB中正确链接了tableView吗? – Pei

+0

显示如何填充表格。 –

回答

0

你说你正在使用核心数据,但你实际上并没有观察到那里的变化(比如通过使用提取的结果控制器)。您正在设置_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"); 
    }); 
} 
+0

我以前试过这个,但它只是空白了整个tableView出于某种原因 – GangstaGraham

+0

这表明问题确实与findAllSortedBy或GGUser currentUser ... – Wain

+0

我的heroku服务器再次工作,所以我会通过使用断点来看看它是怎么回事,findAllSortedby可能是正确的,但因为它是GitHub – GangstaGraham