Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CloudKit私有数据库返回前100条记录_Ios_Swift_Cloudkit - Fatal编程技术网

Ios CloudKit私有数据库返回前100条记录

Ios CloudKit私有数据库返回前100条记录,ios,swift,cloudkit,Ios,Swift,Cloudkit,我在使用CloudKit时面临这种问题。正在尝试从“数据”记录中获取所有数据。结果限制为100。如何获取所有数据?谢谢你的建议 func getAllDataFromCloudKit(){ let predicate = NSPredicate(value: true) let container = CKContainer.defaultContainer() let privateDatabase = container.privateCloudDatabase

我在使用CloudKit时面临这种问题。正在尝试从“数据”记录中获取所有数据。结果限制为100。如何获取所有数据?谢谢你的建议

func getAllDataFromCloudKit(){
    let predicate = NSPredicate(value: true)
    let container = CKContainer.defaultContainer()
    let privateDatabase = container.privateCloudDatabase
    let query = CKQuery(recordType: "Data", predicate: predicate)

    privateDatabase.performQuery(query, inZoneWithID: nil) { results, error in
        if error != nil {
            print(error)
        }
        else {
            for result in results! {
                // return only 100 first 
            }
        }
    }
}
另外,我发现了一个类似的问题,仍然不清楚,或者答案太旧,不能用于新的Swift版本

编辑:参见下面我的最终解决方案如何从私有数据库获取所有数据:

Kevin

使用CKQueryOperation返回的游标;这在很大程度上是数据库领域的标准方法;例如,我知道一些dropbox操作使用相同的方法。以下是CKQueryOperation的基本代码

func query4Cloud(theLink: String, theCount: Int) {
    var starCount:Int = 0
    let container = CKContainer(identifier: "iCloud.ch")
    let publicDB = container.publicCloudDatabase
    let singleLink2LinkthemALL = CKRecordID(recordName: theLink)
    let recordToMatch = CKReference(recordID: singleLink2LinkthemALL, action: .DeleteSelf)
    let predicate = NSPredicate(format:"theLink == %@", recordToMatch)
    let query = CKQuery(recordType: "Files", predicate: predicate)

    // You run the query operation replacing query with your cursor

    readerOperation = CKQueryOperation(query: query)
    readerOperation.desiredKeys = ["record.recordID.recordName"];

    readerOperation.recordFetchedBlock = { (record) in
        starCount += 1
    }

   // see cursor here, if it is nil than you have no more records
   // if it has a value than you have more records to get

    readerOperation.queryCompletionBlock = {(cursor, error) in
            print("fcuk query4Cloud \(theLink) \(theCount) \(starCount)" )
            if error != nil {
                 self.showAlert(message: error!.localizedDescription)
                print("ting, busted")
            } else {
                // it's done
            }

    }
    print("publicDB.addOperation(operation)")
    readerOperation.qualityOfService = .Background
    publicDB.addOperation(readerOperation)
}

好的,我找到了解决办法。见下文:

func loadDataFromCloudKit() {
    var results: [AnyObject] = []
    let cloudContainer = CKContainer.defaultContainer()
    let privateDatabase = cloudContainer.privateCloudDatabase

    let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: "Data", predicate: predicate)
    query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

    let queryOperation = CKQueryOperation(query: query)
    queryOperation.desiredKeys = ["id","name"]
    queryOperation.queuePriority = .VeryHigh
    // Max limit is still 100
    queryOperation.resultsLimit = 100

    queryOperation.recordFetchedBlock = { (record:CKRecord!) -> Void in
        results.append(record)
    }

    queryOperation.queryCompletionBlock = { (cursor, error) in
        dispatch_async(dispatch_get_main_queue()) {
            if (error != nil) {
                print("Failed to get data from iCloud - \(error!.localizedDescription)")
            } else {
                print("Successfully retrieve the data form iCloud")

            }
        }
        // see cursor here, if it is nil than you have no more records
        // if it has a value than you have more records to get
        if cursor != nil {
            print("there is more data to fetch")
            let newOperation = CKQueryOperation(cursor: cursor!)
            newOperation.recordFetchedBlock = { (record:CKRecord!) -> Void in
                results.append(record)
            }

            newOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
            privateDatabase.addOperation(newOperation)
        } else {
            // gets more then 100
            print(results.count)
        }
    }
    privateDatabase.addOperation(queryOperation)
}

您应该使用
CKQueryOperation
而不是
performQuery
。你有更多的控制权,你可以处理更多的记录。在我看来,这并不是很基本。我只需要读取所有私有数据库记录。无论如何,谢谢你。我们将努力实施。