向下滚动加载更多记录

时间:2016-04-04 21:11:26

标签: swift cloudkit

loadPost函数最初将使用查询操作的resultsLimit加载12条记录。接下来的12条记录将加载光标以再次调用批次的其余部分。

我的问题是如何在视图向下滚动后如何加载接下来的12条记录呢?

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

1 个答案:

答案 0 :(得分:0)

安德鲁,

您偶然发现了使用云套件的一个最有趣的方面,即使用延迟和线程进行编程。为了实现您的目标,您需要使用像大型中央调度块这样的设施。

使用其中一个启动第二个步骤,同时执行初始查询以继续加载/预加载更多/后检查加载完成。

我在这里发布了一个块操作模板,在这里。

How to determine when all images have been downloaded from a set in Swift?

除非你准备好,否则不要让你的用户在此期间滚动;如果他们试图给他们一个友好的友好消息,告诉他们你等待cloudkit给你数据。