जब उपयोगकर्ता मेरे संग्रह व्यूपोकेमॉन के अंत तक पहुंचता है तो मेरा लक्ष्य अधिक जानकारी लोड होता है, लेकिन getPokes
func जिसे मैं इसे प्राप्त करने के लिए उपयोग कर रहा हूं, को एक्स मात्रा कहा जा रहा है, जबकि जानकारी प्राप्त करने के लिए मैंने लागू किए गए कथन के अनुरूप अन्य अंतिम बिंदुओं से। यह उस व्यवहार से बहुत दूर है जिसकी मुझे उम्मीद थी।
इस तरह मेरा कोड दिखता है:
सेवा वर्ग
class Service {
let baseUrl = "https://pokeapi.co/api/v2/pokemon"
let limit = 20
var offset = 0
var isPaginating = false
func getPokes(pagination: Bool = false,
handler: @escaping ([Pokemon]) -> Void) {
let topCalls = (limit*5)-limit
let originalPokes = [Pokemon]()
let morePokemons = [Pokemon]()
var endpoint = "\(baseUrl)/?limit=\(limit)&offset=\(offset)"
if pagination {
while offset < topCalls {
isPaginating = true
offset += limit
endpoint = "\(baseUrl)/?limit=\(limit)&offset=\(offset)"
self.mainApiCall(mainList: morePokemons, endpoint: endpoint) { (result) in
switch result {
case .success(let pokemons):
handler(pokemons)
if pagination {
self.isPaginating = true
}
case .failure(let error):
print(error)
}
}
}
} else {
self.mainApiCall(mainList: originalPokes, endpoint: endpoint) { (result) in
switch result {
case .success(let pokemons):
handler(pokemons)
case .failure(let error):
print(error)
}
}
}
}
ViewController वर्ग (गुण)
class ViewController: UIViewController,
UICollectionViewDelegateFlowLayout,
UICollectionViewDelegate,
UICollectionViewDataSource,
UIScrollViewDelegate {
private let service = Service()
private var pagingChecker = false
var generalList = [Pokemon]()
var pokemons = [Pokemon]()
var morePokemons = [Pokemon]()
. . .
}
viewDidLoad विधि (यह अब तक ठीक है)
override func viewDidLoad() {
super.viewDidLoad()
setViews()
service.getPokes(pagination: false) { [self] (pokes) in
pokemons = pokes
generalList += pokemons
DispatchQueue.main.async {
collectionViewPokemon?.reloadData()
}
}
}
scrollViewDidScroll विधि (यहां त्रुटि है)
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let position = scrollView.contentOffset.y
if position > ((collectionViewPokemon!.contentSize.height) - 200 - scrollView.frame.size.height) {
guard !service.isPaginating else { return }
if !pagingChecker {
service.getPokes(pagination: true) { [self] (morePokes) in
morePokemons = morePokes
generalList += morePokemons
DispatchQueue.main.async {
collectionViewPokemon?.reloadData()
}
}
} else {
pagingChecker = true
print("Done paging.")
}
}
}
बात यह है कि मैं एक समय में पोकेमॉन की सिर्फ एक सरणी लाना चाहता हूं, लेकिन इसके बजाय मैं एक ही समय में सभी समापन बिंदु कॉल कर रहा हूं, और उन्हें अपने सामान्य सूची सरणी में भी जोड़ रहा हूं, जो मेरे संग्रह का मालिक है डेटा स्रोत और वह सब सामान देखें।
पीडी: आईडीके अगर यह मदद करेगा, लेकिन यहां मेरे कंसोल का एक स्क्रीनशॉट है। पहला संदेश ठीक है, लेकिन इसे दूसरों को तभी प्रिंट करना चाहिए जब उपयोगकर्ता संग्रह दृश्य के अंत तक पहुंच जाए, और ठीक है, ऐसा नहीं हो रहा है (इसके बजाय, वे सभी एक ही समय में वापस आ रहे हैं!)
मैं यह नहीं समझ सकता कि इसे कैसे किया जाए। मैं वास्तव में किसी भी सलाह या सुधार की सराहना करता हूं, धन्यवाद।
1 उत्तर
मुझे पता चला कि क्या हो रहा है। मैं अपने एपी कॉल को दोहराने के लिए थोड़ी देर के बयान का उपयोग कर रहा था जब तक कि यह इसकी स्थिति को पूरा करता है (सीमा इसलिए, मैंने उस समय कथन का उपयोग न करने के कुछ तरीकों पर काम किया, और प्रत्येक यूआरएल को बनाने के लिए एक फ़ंक्शन का उपयोग करके समाप्त किया, और फिर, एक चाहिएपेज पैरामीटर सेट अप करें जो जांचता है कि एपीआईकॉल करने की आवश्यकता है या नहीं। इस तरह मेरा कोड समाप्त हुआ: वह मेरी सेवा कक्षा पर है। तो, मेरे कलेक्शन व्यू डेटासोर्स में, मैं सेलफॉरइटमएट पर getPokes (चाहिए पेज: झूठा) को कॉल करता हूं, और विलडिस्प्ले सेल में मैं कहता हूं कि पेज सही है। इससे समस्या ठीक हो गई।let baseUrl = "https://pokeapi.co/api/v2/pokemon"
let limit = 20
var offset = 0
func getPokes(pageNumber: Int = 1,
shouldPage: Bool = true,
handler: @escaping ([Pokemon]) -> Void) {
let originalPokes = [Pokemon]()
let morePokemons = [Pokemon]()
let endpoint = buildEndpoint(shouldPage: shouldPage)
if shouldPage {
mainApiCall(mainList: morePokemons, endpoint: endpoint) { (result) in
switch result {
case .success(let pokemons):
handler(pokemons)
case .failure(let error):
print(error)
}
}
} else {
mainApiCall(mainList: originalPokes, endpoint: endpoint) { (result) in
switch result {
case .success(let pokemons):
handler(pokemons)
case .failure(let error):
print(error)
}
}
}
}
private func buildEndpoint(shouldPage: Bool = false) -> String {
var endpoint = "\(baseUrl)/?limit=\(limit)&offset=\(offset)"
if shouldPage {
offset += limit
endpoint = "\(baseUrl)/?limit=\(limit)&offset=\(offset)"
}
return endpoint
}