简体   繁体   English

退订并重新订阅频道后接收最新的旧PubNub消息

[英]Receiving most recent old PubNub message after unsubscribing and resubscribing to channel

I just started using the PubNub iOS SDK v4.0 in my project. 我刚刚开始在项目中使用PubNub iOS SDK v4.0

The whole goal of using PubNub is a user will be subscribed to a certain channel based on what UIViewController they are currently on. 使用PubNub的总体目标是根据用户当前使用的UIViewController订阅某个频道。

So the user should never be subscribed to or receiving messages from more than one channel at any given time. 因此,在任何给定时间,用户都不应订阅或接收来自多个渠道的消息。

Unfortunately I can't get this to work properly. 不幸的是,我无法使其正常工作。 Here's the example scenario that keeps happening to me: 这是不断发生的示例场景:

I store and setup the client and configuration properties in the App Delegate like this: 我在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;
}

User lands on View Controller A and I subscribe them to Channel_A : 用户登陆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];
}

User leaves View Controller A to go to View Controller B , so I unsubscribe them from Channel A : 用户离开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];
}

I then use the same code structure as above to subscribe the user to Channel_B once they segue to View Controller B . 然后,我使用与上述相同的代码结构在用户选择连接到View Controller B后将其订阅到Channel_B

I publish 2 new PubNub messages on View Controller B , one goes to Channel B and another one goes to Channel_A . 我在View Controller B上发布了2条新的PubNub消息,一条消息发送到Channel B ,另一条消息发送到Channel_A The current user only receives the message for Channel_B , but any other users on the app currently on View Controller A will receive the Channel_A message. 当前用户仅收到有关Channel_B的消息,但是当前位于View Controller A上的应用程序上的任何其他用户将收到Channel_A消息。

This almost works perfectly. 这几乎完美地工作。 The current user on View Controller B only receives the Channel_B message, but here's where I run into problems. View Controller B上的当前用户仅收到Channel_B消息,但这是我遇到问题的地方。

When the user leaves View Controller B and goes back to View Controller A , they instantly receive the most recent message that was just posted moments ago from View Controller B for Channel_A . 当用户离开View Controller B并返回到View Controller A ,他们会立即收到刚才从View Controller BChannel_A最新消息。

I don't understand why they are receiving this message when they were unsubscribed from Channel_A while on View Controller B . 我不明白为什么在View Controller B上取消订阅Channel_A时为什么收到此消息。

I have even tested it and waited a minute to pop back to View Controller A , and I still always receive the most recent Channel_A message that was posted a minute ago. 我什至已经对其进行了测试,并且等待了一分钟后弹出到View Controller A ,并且我仍然始终会收到一分钟前发布的最新Channel_A消息。

I don't want this to happen. 我不希望这种情况发生。 They should only receive real time messages for that channel, not one that happened 10 seconds ago, 30 seconds ago, etc. when they were unsubscribed from that channel. 他们应该只收到该频道的实时消息,而不是在取消订阅该频道时10秒钟,30秒钟之前发生的消息。

I did some quick research and thought setting the following properties in the App Delegate might help, but I still experience this problem: 我进行了一些快速研究,认为在App Delegate设置以下属性可能会有所帮助,但我仍然遇到此问题:

self.configuration.restoreSubscription = NO;
self.configuration.catchUpOnSubscriptionRestore = NO;

Just figured it out. 只是想通了。 I had to add the following line of code for my configuration: 我必须为配置添加以下代码行:

self.configuration.keepTimeTokenOnListChange = NO;

By default this is set to YES and will retrieve the most recent message when subscribing to a channel. 默认情况下,此选项设置为YES并且在订阅频道时将检索最新消息。

I also was able to stop setting catchUpOnSubscriptionRestore to NO but still had to keep setting restoreSubscription to NO . 我还能够停止将catchUpOnSubscriptionRestore设置为NO但仍然必须继续将restoreSubscription设置为NO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM