Ios 分页直到结束

Ios 分页直到结束,ios,swift,pubnub,Ios,Swift,Pubnub,我有一个应用程序,它使用PubNub作为聊天服务。登录后,我想下载所有历史信息。不幸的是,PubNub将消息的最大数量限制为100条,因此您必须使用分页来下载所有消息,直到没有更多消息到达为止。 我希望实现的工作流程如下: client.historyForChannel(channel, start: nil, end: nil, includeTimeToken: false) { (result, status) in // my code here... } 加载前100条

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

client.historyForChannel(channel, start: nil, end: nil, includeTimeToken: false)
{ (result, status) in
      // my code here...
}
  • 加载前100条消息
  • 处理它们(在应用程序中存储)
  • 加载接下来的100条消息
  • 等等
  • 他们提供的方法如下:

    client.historyForChannel(channel, start: nil, end: nil, includeTimeToken: false)
    { (result, status) in
          // my code here...
    }
    
    问题是,在“我的代码在这里…”部分,我需要再次调用该函数(使用startDate)来加载接下来的100条消息。但要再次调用函数,我需要设置一个完成块,它的作用与调用函数的完成块完全相同。这是一个无限循环。。我如何以不同的方式解决这个问题?多谢各位

    PubNub历史分页示例代码 所有SDK都有实现历史分页的示例代码。请参阅

    以下是内联代码:

    分页历史记录响应: 您可以通过传递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];
                 */
            }
        })
    }
    
    //取出所有比1439501270438477发送的消息更新的消息。
    let date=NSNumber(值:(1439501270438477为CUnsignedLongLong));
    self.historyNewerThen(日期,onChannel:“history\u channel”,完成:{(messages)in
    打印(“来自历史记录的消息:\(消息)”)
    })
    func historyNewerThen(uu日期:NSNumber,单通道通道:String,
    withCompletion闭包:@escaping(数组)->Void){
    变量msgs:Array=[]
    self.historyNewerThen(日期,onChannel:channel,withProgress:{(消息)in
    附加(内容:消息)
    如果messages.count<100{closure(msgs)}
    })
    }
    private func history newerthen(uu日期:NSNumber,单通道通道:String,
    withProgress闭包:@escaping(数组)->Void){
    self.client?.historyForChannel(频道,开始:日期,结束:无,限制:100,
    反向:false,完成:{(结果,状态)在
    如果状态==nil{
    关闭((结果?.data.messages)!)
    如果结果为?.data.messages.count==100{
    self.historyNewerThen((result?.data.end)!,onChannel:channel,
    (进度:关闭)
    }
    }
    否则{
    /**
    处理邮件历史记录下载错误。请检查“类别”属性
    找出请求失败的可能原因。
    查看状态的“errorData”属性(具有PNErrorData数据类型)
    对象以获取有关此问题的其他信息。
    可以使用:[状态重试]重新发送请求;
    */
    }
    })
    }
    

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

    “不幸的是,PubNub将最大消息数限制为100”-事实上,幸运的是,否则您可能会等待数百万条消息;)但我同意PubNub可以提供一个开发人员朋友API,通过历史记录检索分页消息。这里的关键是有一个终止条件,当它被传递到函数/方法的每个递归调用中时,可以对其进行检查。请继续关注这个问题的答案。看来我们有一个解决方案,但不是Swift。目前,该服务器可以作为伪代码使用,但已向我们的文档团队提交正式请求,以提供iOS。请让我知道这是否符合您目前的目的,或者您是否需要在Swift中实现此代码的进一步帮助。以下是我们的iOS工程师目前提供的一些示例代码:有关官方文档和代码,请参阅下面的答案。干杯!我们正在研究更好的方法来展示我们所有的文档。有这么多SDK有点复杂,但一些重构是在不久的将来。