loadPost işlevi, başlangıçta sorgu işleminin resultsLimit değeriyle 12 kaydı yükleyecektir. Sonraki 12 kayıt, partinin geri kalanını tekrar 12 ile sınırlamak için imlecin içine yüklenir., aşağıya doğru daha fazla kayıt yükleme
Sorum şu ki, sonraki 12 kaydı yalnızca görünüm aşağı kaydırıldıktan sonra nasıl yüklerim?
import UIKit
import CloudKit
class homesVC: UICollectionViewController {
var app : [CKRecord]
var backgroundQueue : NSOperationQueue
required init?(coder aDecoder: NSCoder) {
app = []
backgroundQueue = NSOperationQueue()
super.init(coder: aDecoder)
}
var refresher: UIRefreshControl!
var page: Int = 12
var uuidArray = [String]()
var picArray = [CKAsset]()
override func viewDidLoad() {
super.viewDidLoad()
//pull to refresh
refresher = UIRefreshControl()
refresher.addTarget(self, action: #selector(homesVC.refresh), forControlEvents: UIControlEvents.ValueChanged)
collectionView?.addSubview(refresher)
//load posts func
loadPosts()
}
//load posts func
func loadPosts(){
let container = CKContainer.defaultContainer()
let database = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "posts", predicate: predicate))
let queryOperation = CKQueryOperation(query: query)
queryOperation.resultsLimit = 12
queryOperation.recordFetchedBlock = {
record in
self.app.append(record)
self.uuidArray.append(record.valueForKey("uuid") as! String)
self.picArray.append(record.valueForKey("pic") as! CKAsset)
NSOperationQueue.mainQueue().addOperationWithBlock(){
self.collectionView?.reloadData()
}
}
queryOperation.queryCompletionBlock = {
cursor, error in
if error != nil {
print(error!.localizedDescription)
} else{
if cursor != nil {
self.loadMore(cursor!)
}
}
}
}
cloudKitCentral.publicDatabase.addOperation(queryOperation)
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y >= scrollView.contentSize.height - self.view.frame.size.height{
}
}
func loadMore(cursor: CKQueryCursor){
let queryOperation = CKQueryOperation(cursor: cursor)
queryOperation.resultsLimit = 12
queryOperation.recordFetchedBlock = {
record in
self.app.append(record)
self.uuidArray.append(record.valueForKey("uuid") as! String)
self.picArray.append(record.valueForKey("pic") as! CKAsset)
}
queryOperation.queryCompletionBlock = {
cursor, error in
if error != nil {
print(error?.localizedDescription)
} else {
if cursor != nil {
print("total records: \(self.app.count)")
self.loadMore(cursor!)
} else {
print("completed query")
NSOperationQueue.mainQueue().addOperationWithBlock(){
self.collectionView?.reloadData()
}
}
}
}
container.publicDatabase.addOperation(queryOperation)
}