I just started using the PubNub iOS SDK v4.0
in my project.
The whole goal of using PubNub
is a user will be subscribed to a certain channel based on what UIViewController
they are currently on.
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:
- (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
:
- (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
:
- (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
.
I publish 2 new PubNub
messages on View Controller B
, one goes to Channel B
and another one goes to 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.
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.
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
.
I don't understand why they are receiving this message when they were unsubscribed from Channel_A
while on View Controller B
.
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.
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.
I did some quick research and thought setting the following properties in the App Delegate
might help, but I still experience this problem:
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.
I also was able to stop setting catchUpOnSubscriptionRestore
to NO
but still had to keep setting restoreSubscription
to NO
.