我创建了一个日历应用,我需要跨设备同步事件。我搜索并找到了这个教程: http://code.tutsplus.com/tutorials/building-a-shopping-list-application-with-cloudkit-introduction--cms-24674
然后设置此代码:
private func fetchUserRecordID() {
// Fetch Default Container
let defaultContainer = CKContainer.defaultContainer()
// Fetch User Record
defaultContainer.fetchUserRecordIDWithCompletionHandler { (recordID, error) -> Void in
if let responseError = error {
print(responseError)
} else if let userRecordID = recordID {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.fetchUserRecord(userRecordID)
})
}
}
}
private func fetchUserRecord(recordID: CKRecordID) {
// Fetch Default Container
let defaultContainer = CKContainer.defaultContainer()
// Fetch Private Database
let privateDatabase = defaultContainer.privateCloudDatabase
// Fetch User Record
privateDatabase.fetchRecordWithID(recordID) { (record, error) -> Void in
if let responseError = error {
print(responseError)
} else if let userRecord = record {
print(userRecord)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Fetch User Record ID
fetchUserRecordID()
}
当app运行时,编译器会给我这个输出:
<CKRecord: 0x13f549090; recordType=Users, recordID=_3c16e121bd62445ae4f98b93c1488025:(_defaultZone:__defaultOwner__), recordChangeTag=1>
我在iPhone上创建了一个事件,然后在iPad上打开应用程序,但没有任何反应。
这是我的核心数据模型: