daspianist
daspianist

Reputation: 5495

Retrieving the latest 10 messages from PubNub server

Per the latest PubNub 3.6.2 documentation, I am using the the following method to retrieve the latest 10 messages sent in a channel:

PNDate *now = [PNDate dateWithDate:[NSDate date]];
[PubNub requestHistoryForChannel:self.currentChannel from:nil to:now limit:10 reverseHistory:YES includingTimeToken:YES withCompletionBlock:^(NSArray *retrivedMessages, PNChannel *channel, PNDate *beginDate, PNDate *endDate, PNError *error) {
...}];

My issue is, after 10 messages have been sent in this channel, this method will only retrieve the first 10 messages, and not the latest 10 messages. I thought using from:nil to:now would always be getting the latest messages, and was wondering if there is something I missed?

Thanks

Upvotes: 2

Views: 387

Answers (1)

user3191512
user3191512

Reputation: 46

daspianist,

The reason you are getting the first 10 Messages is that you are have specified reverseHistory:YES. This has the effect of traversing message history starting with the oldest message first.

For example if my message queue is: 1,2,3,4,5,6,8,9,10 (Where 1 is the oldest message and 10 is the latest message).

[PubNub requestHistoryForChannel:self.currentChannel from:nil to:now limit:3 reverseHistory:YES includingTimeToken:YES withCompletionBlock:^(NSArray *retrivedMessages, PNChannel *channel, PNDate *beginDate, PNDate *endDate, PNError *error) { ...}];

Should return: [1,2,3]

By setting reverseHistory:NO, the call should the most recent messages in the queue.

So based on the previous example:

[PubNub requestHistoryForChannel:self.currentChannel from:nil to:now limit:3 reverseHistory:NO includingTimeToken:YES withCompletionBlock:^(NSArray *retrivedMessages, PNChannel *channel, PNDate *beginDate, PNDate *endDate, PNError *error) { ...}];

Should return: [8,9,10]

Upvotes: 3

Related Questions