Simple use of CKDatabase
Database = CKContainer.default the let () publicCloudDatabase. // CKContainer: Containers like sandbox application running as an application sandbox can only access their own content in other applications can not access the override FUNC the viewDidLoad () { super.viewDidLoad () the let Volvo = CKRecord.init (the recordType: "PERSON1") volvo.setObject ( "Liu" AS __CKRecordObjCValue, forKey: "name") volvo.setObject (AS __CKRecordObjCValue 18 is, forKey: "Age") volvo.setObject (to false __CKRecordObjCValue AS, forKey: "isMan") database.save (Volvo) {(the Record, error) in IF error == nil { Print ( "success \ (the Record .Object (forKey:?" name "))") } the else { Print ( "failed") } } }
Demo, create a zone record
the ViewController class: the UIViewController { the let = CKContainer.default Database () privateCloudDatabase. enum carType: String { Case Hatckback = "Hatckback" Case Estate = "Estate" FUNC ZoneID ID of () -> CKRecordZone.ID { // CKOwnerDefaultName the let = ZoneID ID of CKRecordZone. ID.init (zonename: self.rawValue, ownerName: CKRecordZone.ID.defaultZoneName) return ZoneID ID of } FUNC zone () -> CKRecordZone { return (ZoneID ID of: self.zoneId ()) CKRecordZone.init } } // generates a region of record func carWithType(type:CarType)->CKRecord{ let uuid = NSUUID (). uuidString // success of a random identifier, just from the name, do not need to use uuid the let recordld = CKRecord.ID.init (RecordName: UUID, ZoneID ID of: type.zoneId ()) // producing region name let car = CKRecord.init (recordType: " MyCar", recordID: recordId) // MyCar MyCar want a specialty model return CAR } // assignment to record func carWithType (type: carType, maker : String, model: String, numberOfDoors: Int, year: Int) -> {CKRecord the let = carWithType record (type: type); // generates a recording record.setValue (maker, forKey: "maker "); // copy to the recording record.setValue ( Model, forKey: "Model"); record.setValue (numberOfDoors, forKey: "numberOfDoors"); record.setValue (year, forKey: "year"); return record } // returns a recording area assigned func hatchbackCarWithMaker(maker: String,model: String,numberOfDoors: Int, year: Int) -> CKRecord{ return carWithType(type: .Hatckback, maker: maker, model: model, numberOfDoors: numberOfDoors, year: year) } func estateCarWithMaker(maker: String, model: String, numberOfDoors: Int, year: Int) -> CKRecord{ return carWithType(type: .Estate, maker: maker, model: model, numberOfDoors: numberOfDoors, year: year) } // func saveCarClosure(record: CKRecord! , error: CKError! ){ /* 小心,可能在非UI线程中 */ if error != nil{ // error as! CKError print("Failed to save the car. Error = \(String(describing: error)) ::::\(String(describing: error.retryAfterSeconds))") }else{ print("Successfully saved the car with type \(record.recordType)") } } //保存多条记录 func saveCars(cars: [CKRecord]){ for car in cars{ database.save(car) {[weak self] (record, error) in self?.saveCarClosure(record: record, error: error as? CKError) } } } func saveEstateCars(){ let volvoV50 = estateCarWithMaker(maker: "Volvo", model: "V50", numberOfDoors: 5, year: 2016); let audiA6 = estateCarWithMaker(maker: "Audi", model: "A6", numberOfDoors: 5, year: 2018); let skodaOctavia = estateCarWithMaker(maker: "Skoda", model: "Octavia", numberOfDoors: 5, year: 2016); print("Saving estate cars...") saveCars(cars: [volvoV50, audiA6, skodaOctavia]) } func saveHatchbackCars(){ let fordFocus = hatchbackCarWithMaker(maker: "Formaker: d", model: "Focus", numberOfDoors: 6, year: 2018) print("Saving hatchback cars...") saveCars(cars: [fordFocus]) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear (Animated) // check if the user logged into iCloud account isIcloudAvailable IF () == false { print("iCloud is not available.") return } // use the data fetchAllRecordZones plug-in user's private database already exist, create it, then save the data, if it is created, you must create a zone database.fetchAllRecordZones {[weak self] (zones , error) in error IF! = nil { Print ( "Could Not Retrieve The Zones") } the else { var = foundEstateZone to false var = foundHatchbackZone to false for Zone in Zones AS! [CKRecordZone] { IF zone.zoneID.zoneName == CarType.Hatckback.rawValue { foundHatchbackZone = to true } == CarType.Estate.rawValue the else {IF zone.zoneID.zoneName foundEstateZone to true = } } self! .useOrSaveZone(zoneIsCreatedAlready: foundEstateZone, forCarType: .Estate) // self! .useOrSaveZone(zoneIsCreatedAlready: foundHatchbackZone, forCarType: .Hatckback) } } } //检查用户是否登录了iCloud账号 func isIcloudAvailable() -> Bool{ print(FileManager.default.ubiquityIdentityToken) if let token = FileManager.default.ubiquityIdentityToken{ return true } else { return false } } func saveCarsForType(type: CarType){ switch type{ case .Hatckback: saveHatchbackCars() case .Estate: saveEstateCars() default: print("Unknown car state is given") } } // zoneIsCreatedAlready区域是否存在 func useOrSaveZone(zoneIsCreatedAlready: Bool, forCarType: CarType){ if zoneIsCreatedAlready{ print("Found the \(forCarType.rawValue) ,z,o,n,e. It' s been created already") saveCarsForType(type: forCarType) } else { database.save(forCarType.zone()) {[weak self] (zone, error) in if error != nil{ print("Could not save the hatchback zone.\(String(describing: error)) ") }else{ print("Successfully saved the hatchback zon") self?.saveCarsForType(type: forCarType) // print("Successfully saved the hatchback zon") } } } } }