【发布时间】:2014-07-31 07:51:30
【问题描述】:
如何使用 History API 接收频道的历史记录?在文档中,它说对[PubNub requestHistoryForChannel:myChannel from:nil to:nil limit:100 reverseHistory:YES]; 的调用在成功时返回一个数组,但我的编译器断言返回值是无效的。该方法和相关方法的文档说它们“从历史中获取消息”,但我似乎无法弄清楚消息被提取到哪里。是否有将消息发送到的委托方法?请帮帮我。
谢谢
编辑
现在正在接收消息,但不是按照指定的时间间隔。我正在接收频道上的所有消息。
- (void)subscribePostChannels:(NSArray *)results withError:(NSError *)error
{
if (!error) {
static int SECONDS_IN_TEN_DAYS = 864000;
for (PFObject *post in results) {
if ([post.createdAt timeIntervalSinceNow] > (-1) * SECONDS_IN_TEN_DAYS) {
[self.channelsToSubscribe addObject:post.objectId];
NSString *pushChannel = [NSString stringWithFormat:@"channel_%@", post.objectId];
[[PFInstallation currentInstallation] addUniqueObject:pushChannel forKey:@"channels"];
}
}
NSArray *channels = [PNChannel channelsWithNames:self.channelsToSubscribe];
[PubNub subscribeOnChannels:channels];
// Now retrieve messages
NSDate *lastLogin = [PFUser currentUser][@"lastActive"];
for (PNChannel *channel in channels) {
[PubNub requestHistoryForChannel:channel from:[PNDate dateWithDate:lastLogin] includingTimeToken:YES withCompletionBlock:^(NSArray *array, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {
if (!error) {
NSLog(@"Last Active: %@", [PFUser currentUser][@"lastActive"]);
if (channel == [channels lastObject]) {
[PFUser currentUser][@"lastActive"] = [NSDate date];
[[PFUser currentUser] saveInBackground];
}
} else {
NSLog(@"Error Fetching History: %@", error);
}
}];
}
} else {
NSLog(@"Error finding messages. Post channels not subscribed.");
}
}
- (void)subscribePostChannels:(NSArray *)results withError:(NSError *)error
{
static int SECONDS_IN_TEN_DAYS = 864000;
for (PFObject *post in results) {
if ([post.createdAt timeIntervalSinceNow] > (-1) * SECONDS_IN_TEN_DAYS) {
[self.channelsToSubscribe addObject:post.objectId];
NSString *pushChannel = [NSString stringWithFormat:@"channel_%@", post.objectId];
[[PFInstallation currentInstallation] addUniqueObject:pushChannel forKey:@"channels"];
}
}
NSArray *channels = [PNChannel channelsWithNames:self.channelsToSubscribe];
[PubNub subscribeOnChannels:channels];
// Now retrieve messages
for (PNChannel *channel in channels) {
NSDate *lastLogin = [PFUser currentUser][@"lastActive"];
[PubNub requestHistoryForChannel:channel from:[PNDate dateWithDate:lastLogin] to:nil];
}
}
日志消息表明 lastActive 发生在从历史记录中接收到的消息之后。
【问题讨论】:
-
你正在运行什么代码调用 subscribePostChannels: withError:?
-
我在上面的代码中看到的主要问题:1)订阅将不会运行,除非您已经通过 connect() 方法连接到 PubNub。您是否已经在未提供的代码中连接到其他地方? 2)如果您已经连接,那么您嵌入历史调用的连接完成块将永远不会执行,因为您已经连接。
-
是否将 [PubNub requestHistoryForChannel:channel 从:[PNDate dateWithDate:lastLogin] 移动到:nil];从连接成功块内到连接块外,在您的订阅调用之后,让一切正常吗?
-
是的,我已经连接上了。我在 applicationDidFinishLaunchingWithOptions 中连接第一件事
-
好的,如果您已经连接,那么您必须从第二个连接请求的成功块中删除历史调用。这是否为您解决了问题?