在Swift中使用CloudKit获取记录

时间:2014-08-10 22:45:33

标签: ios swift records cloudkit

我尝试使用CloudKit获取记录。我使用Apple的例子。

我可以保存记录,但我无法得到它们。

这里是获取保存记录的代码:

func fetchRecords(completionHandler: ((NSArray!) -> Void)!) {
    var predicate = NSPredicate(value: true)
    var query = CKQuery(recordType: "type", predicate: predicate)
    /*self.publicDatabase.performQuery(query, inZoneWithID: nil, { (results:[AnyObject]!, error:NSError!) -> Void in
    if (error){
    println("An error occured in \(NSStringFromSelector(__FUNCTION__)) \(error)");
    } else {
    completionHandler(results)
    }
    })*/

    var operation = CKQueryOperation(query: query)

    var results = NSMutableArray()

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

    operation.queryCompletionBlock = { (cursor:CKQueryCursor!, error:NSError!) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            if (error) {
                // Error Code 11 - Unknown Item: did not find required record type
                if (error.code == 11) {

                    // Since schema is missing, create the schema with demo records and return results

                }
                else {
                    // In your app, this error needs love and care.
                    println("An error occured in \(NSStringFromSelector(__FUNCTION__)): \(error)");
                    abort();
                }
            } else {
                completionHandler(results)
            }
        })
    }

    self.publicDatabase.addOperation(operation)
}

当我运行时," abort()"被调用,我在控制台中得到这个:

fetchRecords中发生错误:

感谢帮助人们。

2 个答案:

答案 0 :(得分:0)

我认为问题在于您创建谓词的方式。未指定要查询的列/属性。

而不是:

var predicate = NSPredicate(value: true)

你应该做的事情是:

var predicate = NSPredicate(format: "someColumn == true")

答案 1 :(得分:0)

尝试替换

var stu= $scope.refresh();
return stu; 

if (error)