【发布时间】:2015-07-26 07:42:45
【问题描述】:
我刚开始在我的项目中使用 PubNub iOS SDK v4.0。
使用PubNub 的整个目标是用户将根据他们当前所在的UIViewController 订阅某个频道。
因此,用户在任何给定时间都不应订阅或接收来自多个频道的消息。
很遗憾,我无法让它正常工作。这是不断发生在我身上的示例场景:
我在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 上发布了两条新的PubNub 消息,一条发送至Channel B,另一条发送至Channel_A。当前用户只收到Channel_B 的消息,但当前在View Controller A 上的应用程序上的任何其他用户都会收到Channel_A 消息。
这几乎可以完美运行。 View Controller B 上的当前用户只收到 Channel_B 消息,但这是我遇到问题的地方。
当用户离开View Controller B 并返回View Controller A 时,他们会立即收到刚刚从View Controller B 为Channel_A 发布的最新消息。
我不明白为什么当他们在View Controller B 上取消订阅Channel_A 时会收到此消息。
我什至已经对其进行了测试,并等待了一分钟才弹回View Controller A,而我仍然总是收到一分钟前发布的最新Channel_A 消息。
我不希望这种情况发生。他们应该只接收该频道的实时消息,而不是在取消订阅该频道时 10 秒前、30 秒前等发生的消息。
我做了一些快速研究,并认为在 App Delegate 中设置以下属性可能会有所帮助,但我仍然遇到这个问题:
self.configuration.restoreSubscription = NO;
self.configuration.catchUpOnSubscriptionRestore = NO;
【问题讨论】:
标签: ios objective-c pubnub