gpt4 book ai didi

iOS - 当新数据发布到 NSMutableArray 时触发通知?

转载 作者:行者123 更新时间:2023-11-29 20:48:57 24 4
gpt4 key购买 nike

我已经在我的服务器上设置了推送通知,一切都运行良好。 例如我可以随时向所有用户发送通知(因为所有设备 token 都存储到我的数据库中)。也就是说,当应用程序中镜像的服务器上的数据更新时(例如当新消息发布到服务器)?

更具体地说:请参阅下面的代码,我用它在表格 View 中显示所有收到的消息。如何才能使登录用户在 self.messages 更新时收到通知?

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *PointsTableIdentifier = @"MyMessagesCell";

MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

}


NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row];

NSString *messageSubject = [receivedSubjectLine objectForKey:@"node_title"];


[cell.subjectLine setText:messageSubject];



NSDictionary *fromUser = [self.messages objectAtIndex:indexPath.row];

NSString *userName = [fromUser objectForKey:@"name"];

[cell.senderName setText:userName];



NSDictionary *receivedBody = [self.messages objectAtIndex:indexPath.row];

NSString *messageBody = [receivedBody objectForKey:@"body"];

[cell.fullMessage setText:messageBody];


NSDictionary *receivedTime = [self.messages objectAtIndex:indexPath.row];

NSString *timeStamp = [receivedTime objectForKey:@"swaptime"];

NSLog(@"The timestamp is %@", timeStamp);

[cell.swapTime setText:timeStamp];

return cell;

}

最佳答案

我的应用程序中有一个聊天不使用Web Socket(我们没有时间实现它),所以我们要做的是使用通知中心刷新UITableView。

在 AppDelegate 中我有以下代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
}

然后在我的聊天类中,我有以下代码在 viewDidLoad 中注册通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadTheTable" object:nil];

然后选择器调用服务器并刷新显示的数据:

- (void)reloadTable:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self checkJson]; //This function loads the data and when it finish calls another method call loadTable
}

- (void)loadTable {
[_tvTable reloadData]; //this simply reload your UITableView
}

我还使用此技术在应用程序进入前台时重新加载:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
}

希望这对您有帮助。

关于iOS - 当新数据发布到 NSMutableArray 时触发通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38214602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com