A dead simple way of Fetching
, Inserting
, Modifying
and Deleting
records from the CloudKit Databases.
A CKRecordContext object holds all the new records, any changes and deletions to them until you call save()
on an instance of it. It only then conveys the changes to the CloudKit Database.
var cksRecordContext:CKSRecordContext = CKRecordContext(database: CKContainer.defaultContainer().privateCloudDatabase, recordZone: nil)
var ckRecord = cksRecordContext.insertNewCKRecord("NewRecordType")
Any CKRecords you create using methods of CKRecordContext are monitored by it.
var ckRecord = cksRecordContext.insertNewCKRecord("NewRecordType")
ckRecordContext.deleteRecord(record: ckRecord)
OR
var ckRecord = cksRecordContext.insertNewCKRecord("NewRecordType")
ckRecordContext.deleteRecord(recordID: ckRecord.recordID)
func fetchCKRecord(recordID:CKRecordID,completion:(record:CKRecord?,error:NSError!) ->())
func fetchCKRecords(recordType:String,predicate:NSPredicate,completion:(results:Array<AnyObject>?,error:NSError!) ->())
func fetchCKRecords(recordType:String,predicate:NSPredicate,sortDescriptors:[NSSortDescriptor],completion:(results:Array<AnyObject>?,error:NSError!) ->())
Its a one liner !
cksRecordContext.save { (error) -> (Void) in
if error != nil
{
print("Saved Successfully")
}
}
Check out the sample iOS demo app.
CocoaPods
is the recommended way of adding CKSRecordContext to your project.
You want to to add pod 'CKSRecordContext', '~> 0.5'
similar to the following to your Podfile:
target 'MyApp' do
pod 'CKSRecordContext', '~> 0.5'
end
Then run a [sudo] pod install
inside your terminal, or from CocoaPods.app.
CKSRecordContext was created by Nofel Mahmood
Follow Nofel Mahmood on Twitter and GitHub or email him at nofelmehmood@gmail.com
CKSRecordContext is available under the MIT license. See the LICENSE file for more info.