【问题标题】:PromiseKit with paging in swift快速分页的 PromiseKit
【发布时间】:2018-05-07 10:36:50
【问题描述】:

我正在使用 https://github.com/mxcl/PromiseKit 设置 promosekit 以链接 API 调用。

我对如何在 promisekit 中实现分页有疑问。

【问题讨论】:

  • 你的问题是什么?
  • 我在 promise kit 中实现分页时遇到问题,我们将第 1 页作为参数传递,然后它是返回日期并链接第 2 页,直到所有页面都被获取
  • 你能否用这个细节和一些代码更新你的问题。没有明确和详细的说明,你就无法得到答案。

标签: ios swift promisekit


【解决方案1】:

不要使用PromiseKit,而是使用OperationQueue。而不是魔法,你会理解它的每一点。

显然它需要比PromiseKit 更多的代码,但它是在 Apple SDK 中提供的。

请检查以下sn-p

// setup your operation queue
// typically done where you start sending the api request
// like viewDidLoad
// assumed that opQueue is declared as class's instance member
opQueue = OperationQueue.init()
opQueue.maxConcurrentOperationCount = 1 // ensure that only one operation will run at a time
opQueue.name = "Api Call Serializer"
opQueue.qualityOfService = QualityOfService.background

// after instantiating add a operation to the queue
opQueue.addOperation {
    makeApiCall(url: first_url)
}


func makeApiCall(url: URL) {
    //put your api call code here
    YOUR_API_CALL({(responseData)// i have assumed that your api call will ending in a swift closure. If you use delegate, it will be same as following.
        // your processing of api response
        // build the next_url for pagination.
        opQueue.addOperation {
            makeApiCall(url: next_url)
        }            
    })
}

希望对你有帮助。

【讨论】:

  • @darshan 如果您在使用这种方法时遇到任何问题,请考虑发表评论。
  • 虽然这篇文章很有用,但我不认为它是无用的,也没有回答这个问题。建议 OP 用 OperationQueue 替换 PromiseKit 也无济于事。想要使用 PromiseKit 的原因有很多(你自己也提到过其中一个)。这个问题虽然说得不好,但它是关于从 API 加载分页数据,该 API 需要某种 start limit。解决方案在这里发布:stackoverflow.com/questions/38404047/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
  • 2017-05-17
  • 2012-05-08
  • 1970-01-01
相关资源
最近更新 更多