我刚开始在我的项目中使用 PubNub iOS SDK v4.0
。
使用 PubNub
的整体目标是用户将根据他们当前使用的 UIViewController
订阅某个 channel 。
因此,用户在任何给定时间都不应订阅或接收来自多个 channel 的消息。
不幸的是,我无法让它正常工作。这是不断发生在我身上的示例场景:
我在 App Delegate
中存储和设置客户端和配置属性,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Pubnub
self.configuration = [PNConfiguration configurationWithPublishKey:pubNubPublishKey
subscribeKey:pubNubSubscribeKey];
self.client = [PubNub clientWithConfiguration:self.configuration];
return YES;
}
用户登陆 View Controller A
,我将他们订阅到 Channel_A
:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// App Delegate
self.appDelegate = [[UIApplication sharedApplication] delegate];
// PubNub
[self.appDelegate.client addListener:self];
[self.appDelegate.client subscribeToChannels:@[@"Channel_A"] withPresence:NO];
}
用户离开 View Controller A
去 View Controller B
,所以我从 Channel A
取消订阅他们:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Remove listener + unsubscribe
[self.appDelegate.client removeListener:self];
[self.appDelegate.client unsubscribeFromChannels:@[@"Channel_A"] withPresence:NO];
}
然后,我使用与上面相同的代码结构在用户转至 View Controller B
时将用户订阅到 Channel_B
。
我在 View Controller B
上发布了 2 条新的 PubNub
消息,一条发送到 Channel B
,另一条发送到 Channel_A
。当前用户仅收到 Channel_B
的消息,但当前在 View Controller A
上的应用程序中的任何其他用户都将收到 Channel_A
消息。
这几乎可以完美运行。 View Controller B
上的当前用户仅接收到 Channel_B
消息,但这是我遇到问题的地方。
当用户离开 View Controller B
并返回到 View Controller A
时,他们会立即收到刚刚从 View 发布的最新消息
。Channel_A
的 Controller B
我不明白为什么他们在 View Controller B
上取消订阅 Channel_A
时会收到此消息。
我什至测试过它并等了一分钟弹出回 View Controller A
,我仍然总是收到一分钟前发布的最新 Channel_A
消息.
我不希望这样的事情发生。他们应该只接收该 channel 的实时消息,而不是 10 秒前、30 秒前等他们取消订阅该 channel 时发生的消息。
我做了一些快速研究,认为在 App Delegate
中设置以下属性可能会有所帮助,但我仍然遇到这个问题:
self.configuration.restoreSubscription = NO;
self.configuration.catchUpOnSubscriptionRestore = NO;
最佳答案
刚刚想通了。我必须为我的配置添加以下代码行:
self.configuration.keepTimeTokenOnListChange = NO;
默认设置为 YES
并在订阅 channel 时检索最新的消息。
我也能够停止将 catchUpOnSubscriptionRestore
设置为 NO
但仍然必须继续将 restoreSubscription
设置为 NO
.
关于ios - 取消订阅和重新订阅 channel 后收到最新的旧 PubNub 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31634773/