【问题标题】:Pagination until the end分页直到结束
【发布时间】:2018-08-05 15:02:47
【问题描述】:

我有一个应用程序使用 PubNub 作为聊天服务。登录后,我想下载所有历史消息。不幸的是,PubNub 将消息的最大数量限制为 100,因此您必须使用分页来下载所有消息,直到没有更多消息到达。 我想实现的工作流程如下:

  1. 加载前 100 条消息
  2. 处理它们(存储在应用程序中)
  3. 加载接下来的 100 条消息
  4. 等等..

他们提供的方法如下:

client.historyForChannel(channel, start: nil, end: nil, includeTimeToken: false)
{ (result, status) in
      // my code here...
}

问题是,在“我的代码在这里...”部分,我需要再次调用该函数(使用 startDate)来加载接下来的 100 条消息。但是要再次调用该函数,我需要设置一个与调用该函数的完成块完全相同的完成块。这是一个无限循环。我怎样才能以不同的方式解决这个问题?非常感谢!

【问题讨论】:

  • “不幸的是,PubNub 将消息的最大数量限制为 100” - 实际上,幸运的是,否则您最终可能会等待数百万条消息;)但同意 PubNub 可以提供更多开发者朋友的 API通过历史记录检索分页消息。这里的关键是要有一个可以检查的终止条件,因为它被传递到函数/方法的每个递归调用中。请继续关注这个问题的答案。
  • 似乎我们有一个 completed example for JS history pagination 但不是 Swift 的。这现在可以作为伪代码服务器,但已向我们的文档团队提交正式请求以提供 iOS。请让我知道这是否适合您现在的目的,或者您是否需要在 Swift 中实现此代码的进一步帮助。
  • 这里是我们 iOS 工程师的一些示例代码:history paging backwardhistory paging foreward
  • 官方文档和代码见下方答案。

标签: ios swift pubnub


【解决方案1】:

PubNub 历史分页示例代码

所有的 SDK 都有实现历史分页的示例代码。请参考PubNub Swift SDK Storage API Reference History Paging section

这是内联的代码:

分页历史响应: 您可以通过传递 0 或有效时间标记作为参数来调用该方法。

// Pull out all messages newer then message sent at 14395051270438477.
let date = NSNumber(value: (14395051270438477 as CUnsignedLongLong));
self.historyNewerThen(date, onChannel: "history_channel", withCompletion:  { (messages) in
     
    print("Messages from history: \(messages)")
})
 
 
func historyNewerThen(_ date: NSNumber, onChannel channel: String, 
                      withCompletion closure: @escaping (Array<Any>) -> Void) {
         
    var msgs: Array<Any> = []
    self.historyNewerThen(date, onChannel: channel, withProgress: { (messages) in
         
        msgs.append(contentsOf: messages)
        if messages.count < 100 { closure(msgs) }
    })
}
     
private func historyNewerThen(_ date: NSNumber, onChannel channel: String, 
                              withProgress closure: @escaping (Array<Any>) -> Void) {
     
    self.client?.historyForChannel(channel, start: date, end: nil, limit: 100, 
                                   reverse: false, withCompletion: { (result, status) in
                                     
        if status == nil {
             
            closure((result?.data.messages)!)
            if result?.data.messages.count == 100 {
                 
                self.historyNewerThen((result?.data.end)!, onChannel: channel, 
                                      withProgress: closure)
            }
        }
        else {
             
            /**
             Handle message history download error. Check 'category' property
             to find out possible reason because of which request did fail.
             Review 'errorData' property (which has PNErrorData data type) of status
             object to get additional information about issue.
              
             Request can be resent using: [status retry];
             */
        }
    })
}

请参阅其他 SDK 语言特定实现中的相同部分。

【讨论】:

  • 干杯!我们正在努力以更好的方式呈现我们拥有的所有文档。有这么多 SDK 有点复杂,但近期会进行一些重构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
相关资源
最近更新 更多