๋ฌธ์
์ํ ๊ฒ์ ํ๋ฉด์์ Pagination์ ์ํด, UIScrollViewDelegate Protocol์ ์ฌ์ฉํ์ฌ scrollView์ offset์ ํ์ฉํด ๊ฒ์ ๊ฒฐ๊ณผ ์ถ๊ฐ ๋ก๋ฉ์ ๊ตฌํํ์๋ค.
offset.y๊ฐ scrollView.contentSize.height (== searchView.collectionView.contentSize.height)์ 67% ์ด์์ด ๋ ๋, ๋ง์ง๋ง์ผ๋ก ๋ก๋ฉํ ํ์ด์ง๊ฐ ์ด ํ์ด์ง ์๋ณด๋ค ์ ์ ๊ฒฝ์ฐ ๋ค์ ํ์ด์ง๋ฅผ API ํต์ ์ ํตํด ๋ฐ์ ์ค๋ ๋ก์ง์ผ๋ก ๊ตฌ์ฑํ์๋ค.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
isPaginating = true
let contentOffset = scrollView.contentOffset.y
let collectionViewHeight = searchView.collectionView.contentSize.height
let remainingHeight = collectionViewHeight * 0.33
// contentSize.height์ ์์ธ์ง ๋๋ฌด ์ปค์(2553.6) remainingHeight์ด collectionViewHeight์ 20%์ผ ๋๋ ๋์ด์ ๋ก๋ฉ์ด ์ ๋จ.
print("๐ contentOffset: \\(contentOffset)")
print("๐ collectionViewHeight: \\(collectionViewHeight)") // 2553.6
print("๐ซ remainingHeight: \\(remainingHeight)") // 1021.44
print("๐ - ๐ซ: \\(collectionViewHeight - remainingHeight)")
// print(page < totalPages)
print("๐ page: \\(page)")
if contentOffset > collectionViewHeight - remainingHeight && page < totalPages {
page += 1
search()
}
}
์ผ๋จ, ๋ค์ ํ์ด์ง๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒ์ด ๊ฐ๋ฅํ๊ธด ํ๋ค..
ํ์ง๋ง scrollViewDidScroll ๋ฉ์๋๋ ์ฌ์ฉ์๊ฐ ์คํฌ๋กค์ ํ ๋๋ง๋ค ํธ์ถ๋๊ณ , ์๊ฐ๋ฝ์ด ํ๋ฉด์ ํฐ์นํ๊ณ ๋ ๋ ํ ๋ฒ ํธ์ถ๋๋ ๊ฒ์ด ์๋๋ผ offset์ด ์กฐ๊ธ์ด๋ผ๋ ๋ฐ๋๋ฉด ํธ์ถ๋๋ ๊ฒ์ ์ ์ ์์๋ค..
API ํต์ ํ response๊ฐ ์ค๊ณ ๋์์ผ collectionView๊ฐ ์ ๋ฐ์ดํธ ๋๋ฉด์ contentSize.height(์ดํ ์ปฌ๋ ์ ๋ทฐ ๋์ด)๊ฐ ๋ฐ๋๋๋ฐ, ๋ณดํต ๊ทธ ์ ์ ์คํฌ๋กค์ด ๊ณ์ ์ด๋ฃจ์ด์ง๋ฏ๋ก ํด๋น ๋ฉ์๋๋ ๊ณ์ ํธ์ถ์ด ๋๊ณ , ์ด ๋ offset.y๋ ์ ๋ฐ์ดํธ ๋๊ธฐ ์ ์ ์ปฌ๋ ์ ๋ทฐ ๋์ด์ 67% ์ด์์ด๋ฏ๋ก ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋๋ง๋ค API ํต์ ์ด ์ผ์ด๋๊ฒ ๋์ด ๊ณผํธ์ถ์ด ๋ฐ์ํ๋ค.
์ด๋ ์์ดํฐ ํ๋ฉด์์ ์คํฌ๋กค๋ฐ์ ์์น๋ฅผ ํตํด ์๊ฐ์ ์ผ๋ก๋ ํ์ธ์ด ๊ฐ๋ฅํ๋ฐ, ์ฒซ ๊ฒ์ ๊ฒฐ๊ณผ ํ์ ํ๋ฉด์์ ์คํฌ๋กค์ ํด์ ๋ค์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ๋ ค ํ๋ฉด, ์คํฌ๋กค์ด ์์์ง๊ณ ์คํฌ๋กค๋ฐ๊ฐ ์์ฒญ๋ ์๋จ..;;์ผ๋ก ์ด๋ํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์คํฌ๋กค๋ฐ๊ฐ ์ด๋ํ ๋๋ ๋ถ๋๋ฝ๊ฒ ์์ง์ด๊ธฐ๋ณด๋ค ๋๋ ๋๊ฒจ (choppy) ๋ณด์ธ๋ค. (์๋ก ์์ฒญ๋๊ฒ ์ ํํ๋๊นใ )
๊ณผํธ์ถ๋ก ์ธํด ๊ณผ๋คํ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๊ฒ ๋๊ณ , ์ด ๊ณผ๋คํ ๋ฐ์ดํฐ๋ฅผ UI๋ก ๋ณด์ฌ์ฃผ์ด์ผ ํ๋ Main Thread๊ฐ ํ ์ผ์ด ๋ง์ ๋งํ๋ ๊ฒ์ด๋ค.. ใ ๊ฒฐ๊ตญ ๋งค๋๋ฝ์ง ๋ชปํ๊ณ ๋๊ฒจ ๋ณด์ด๊ฒ ๋์ด ์ฌ์ฉ์๊ฐ ๋ณด๊ธฐ์๋ ์ฌ์ฉ์ฑ์ด ๋จ์ด์ง๊ฒ ๋๋ค...ใ .ใ
๋ํ ์ด๋ API ๊ณผํธ์ถ๋ก ์ธํ ์๋ฒ๋จ์ ํด๋ผ์ด์ธํธ block์ ์ด๋ํ ์ ์๋ค.. ์ ์ ์ ์ ๋ฃฐ๋ฌ/์์ดํ์ด ๊ณผ๋ค ์ฌ์ฉ์ ๋ค..
ํด๊ฒฐ
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด pagination ๋ฐฉ๋ฒ์ ๋ณ๊ฒฝํ๋ค..!
UICollectionViewDataSourcePrefetching protocol์ ์ฌ์ฉํ์๋ค. prefetching์ ํตํด ์ ์ ํ๋ฉด์ ํ์ํ๊ธฐ ์ ๋ฐ์ดํฐ๋ฅผ ๋ฏธ๋ฆฌ ์์ฒญํ ์ ์๋๋ก ํ๋ค.
extension SearchViewController: UICollectionViewDataSourcePrefetching {
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
for indexPath in indexPaths { // indexPaths: prefetch๋๋ ์
๋ค
if searchResults.count - 1 == indexPath.item && page < totalPages {
isPaginating = true
page += 1
search()
}
}
}
}
prefetch ๋๋ ์ ๋ค ์ค ์ด๋ฏธ ๋ถ๋ฌ์จ ๊ฒ์๊ฒฐ๊ณผ์ ๊ฐ์ฅ ๋ง์ง๋ง์ ํด๋นํ๋ ์ ์์ ๋ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฏธ๋ฆฌ ๋ฐ์์ค๋๋ก ๊ตฌํํ์๊ณ , ์ด๋ ๊ฒ ํ๋ฉด์ ๋ณด์ด๊ธฐ ์ ๋ฏธ๋ฆฌ ๋ก๋ฉ๋ ๋ฐ์ดํฐ๋ UICollectionViewDataSource ํ๋กํ ์ฝ์ cellForItemAt ๋ฉ์๋์์ ํ๋ฉด์ ํ์๋๋ค.
UICollectionViewDataSourcePrefetching protocol์ ์ฌ์ฉํ๋ฉด ์คํฌ๋กค ์ฑ๋ฅ๋ ํฅ์๋๋ค๊ณ ํ๋ค! ๊ทธ๊ฒ์ด ์คํฌ๋กค๋ฐ์ ๋งค๋๋ฌ์ด ์ด๋์ ๋ปํ๋ ๊ฑด์ง๋ ์ ํํ๊ฒ ๋ชจ๋ฅด๊ฒ ์ง๋ง,.. ์ ์ ๋ก์ ๋ณด๊ธฐ์๋ ์ด์ ๋ณด๋ค ํจ์ฌ smoothํ ์คํฌ๋กค๋ฐ๋ฅผ ๋ณผ ์ ์์๋ค..! (๋ค์ด๋ฒ ๊ฒ์์ฒ๋ผ..)
์ฐธ๊ณ
Apple Developer Documentation - UICollectionViewDataSourcePrefetching
Apple Developer Documentation - Prefetching Collection View Data
Apple Developer Documentation
developer.apple.com
Kodeco - UICollectionView Tutorial: Prefetching APIs
UICollectionView Tutorial: Prefetching APIs
In this UICollectionView prefetching tutorial, you’ll learn how to achieve smooth scrolling in your app using Operations and Prefetch APIs.
www.kodeco.com
'๐งฏ Troubleshooting' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Cheliz] View ๊ด๋ จ ์ฌ์ฌ์ฉ์ฑ ๋์ด๊ธฐ (0) | 2023.02.09 |
---|---|
[Cheliz] UITabBarItem์ SF Symbol ์ฌ์ด์ฆ ์กฐ์ Issue (0) | 2023.02.09 |
[Cheliz] Realm ์ค๋ณต ์ถ๊ฐ ๋ฐฉ์ง (0) | 2023.02.09 |
[Cheliz] UICollectionView leading/trailingSwipeActions - Realm Read/Delete (0) | 2023.02.09 |
[Cheliz] UITableView ์ฌ์ฉ์ฑ ํฅ์ (0) | 2023.02.09 |