PromiseKit在swift中进行分页

时间:2018-05-07 10:36:50

标签: ios swift promisekit

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

我有关于如何在promisekit中实现分页的查询。

1 个答案:

答案 0 :(得分:0)

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

显然,它需要的代码多于PromiseKit,但它是Apple SDK内置的。

请查看以下代码段

// 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)
        }            
    })
}

希望它有所帮助。