在 PubNub 的 didReceiveStatus 中获取相关的订阅频道 iOS objective C

Get related subscribed channel(s) in didReceiveStatus in PubNub for iOS objective C

订阅频道后调用 didReceiveStatus 时,我们无法检索刚刚订阅的频道。

PNSubscribeStatus.data.subscribedChannel 或 PNSubscribeStatus.data.actualChannel 始终为空,PNSubscribeStatus.subscribedChannels 给出所有当前订阅的频道,而不是触发 didReceiveStatus 回调的频道。

我们做错了什么?

在 SDK 4.0 中,didReceiveStatus returns a PNStatus,根据 class 文档,除非出现错误情况,否则不包含该额外信息。对于我们的应用程序,我们使用该处理程序来监视与 PubNub 服务器的连接状态。

PubNub 消息在 iOS

中收到频道名称

您应该能够获取接收消息的频道,但能否获取取决于您是订阅了该频道还是订阅了包含该频道的频道组。这是来自 PubNub Objective-C for iOS SDK subscribe API Reference:

的示例代码
- (void)client:(PubNub *)client didReceiveMessage:(PNMessageResult *)message {
  
    // Handle new message stored in message.data.message
    if (message.data.actualChannel) {
  
        // Message has been received on channel group stored in
        // message.data.subscribedChannel
    }
    else {
  
        // Message has been received on channel stored in
        // message.data.subscribedChannel
    }
    NSLog(@"Received message: %@ on channel %@ at %@", message.data.message,
          message.data.subscribedChannel, message.data.timetoken);
}

如果需要客户端订阅的其他频道,可以调用where-now API

如果您需要更加动态地了解 回复 频道应该是什么,那么只需在发布消息时在消息中包含该频道名称,假设发布者有关于这是哪个频道的先验知识。或者您可以及时在您的服务器上查询要回复的频道。

这里是 PubNub 支持的回答:

Actually status.data.subscribedChannel and status.data.actualChannel dedicated to presence events and messages receiving callbacks where information about sources of event is important.

In -client:didReceiveStatus: client doesn’t give information about particular channel on which client has been subscribed. If client will start track this information, there is no guarantee what it will return expected value (as developer expect some channels to be there). In previous version (3.x) all this information has been tracked, but because it can be modified at any moment – result sometimes was unpredictable.

Subscribe can be done in sequence of methods (one after another) like: subscribe A1, subscribe C1, subscribe B1 and B2, unsubscribe C1 and B1 – this as result will end up with single call of -client:didReceiveStatus: with resulting set of channels.

It always best practice just to check whether your channels is in s_tatus.subscribedChannels_.

我的评论: 具有异步过程的要点就是不要将其视为方法序列......我们无法保证订阅以与订阅请求完全相同的顺序完成,除非我们阻止其他订阅请求直到前一个订阅请求完成.