var page_num = 1
guard let url = URL(string: "http://127.0.0.1/api/v4/fetch_records/?page=\(page_num)") else { return }
NetworkManager.GET(url: url)
.decode(type: GlobalData.self, decoder: JSONDecoder())
.subscribe(on: DispatchQueue.global(qos: .background))
.receive(on: DispatchQueue.main)
.sink{ (completion) in
} receiveValue: { [weak self] (returnedData) in
if returnedData.status == "success" {
for record in returnedData.data {
print(record)
}
}
}
來自服務器的資料是這樣的:
{
"page": 1,
"total_pages": 10,
"hasMore": true,
"status": "success",
"data": [{
"id": 23409,
"record_id": "P06TYUQZ",
"money": "1.00",
"date": "2022-10-24",
}]
}
當hasMoreistrue時,page_num = 1并發出下一頁請求,直到hasMoreis false,我該怎么做?希望你們能幫幫我。
uj5u.com熱心網友回復:
您可以嘗試使用遞回方法,像這樣,逐頁獲取所有頁面:
func fetchAllPagesFrom(_ page_num: Int) {
guard let url = URL(string: "http://127.0.0.1/api/v4/fetch_records/?page=\(page_num)") else { return }
// ....
if returnedData.hasMore {
// pace/delay the next call here if need be
fetchAllPagesFrom(page_num 1) // <-- fetch another page
} else {
return // <-- all done
}
}
像這樣使用它:
fetchAllPagesFrom(1)
請注意,Apple 要求https,除非您在您的Info.plist
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/532667.html
標籤:IOS迅速代码迅捷
