Skip to content

Commit

Permalink
Bulk insert and delete plus push notifications handling
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Jan 8, 2016
1 parent aa39ed7 commit b681cbd
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
51 changes: 38 additions & 13 deletions AppMessage/AppMessage/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,58 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

// The 2 commands below are only here to assist during development.

// Only call this line once. It will make sure the recordType are there in iCloud.
// After this, go to the iCloud dashboard and make all metadata for each recordType queryable and sortable!
EVCloudKitDao.publicDB.createRecordTypes([Message(), Asset(), News(), Invoice()])
// If you use this in your own project, then make sure that the fields are not nil otherwise the field will not be created.
// EVCloudKitDao.publicDB.createRecordTypes([Message(), Asset(), News(), Invoice()])

// During development you will probably play around with subscriptins.

// During development you will probably play around with subscriptins.
// To be sure you do not have any old subscriptions left over, just clear them all on startup.
// EVCloudKitDao.publicDB.unsubscribeAll({subscriptioncount in EVLog("subscriptions removed = \(subscriptioncount)")}, errorHandler: {error in })

EVCloudKitDao.publicDB.unsubscribeAll( { count in
EVLog("\(count) subscriptions removed")
})
if EVCloudKitDao.publicDB.accountStatus == .Available {
EVCloudKitDao.publicDB.unsubscribeAll( { count in
EVLog("\(count) subscriptions removed")
})
}

// Here we do not call the EVCloudData.publicDB.fetchChangeNotifications. Instead we do that in the LeftMenuViewController after we are sure that all general .connect are setup

return true
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
// The app is alredy loaded and the .connect calls are setup. So we can process any notifications that have arived and not processed.
func applicationDidBecomeActive(application: UIApplication) {
EVCloudData.publicDB.fetchChangeNotifications({
EVLog("All change notifications are processed")
EVCloudKitDao.publicDB.setBadgeCounter(0)
})
}


// This will only be called when your app is active. Instead we should use the function below
// func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
// EVLog("Push received")
// EVCloudData.publicDB.didReceiveRemoteNotification(userInfo, executeIfNonQuery: {
// EVLog("Not a CloudKit Query notification.")
// }, completed: {
// EVLog("All notifications are processed")
// })
// }


// Process al notifications
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
EVLog("Push received")
EVCloudData.publicDB.didReceiveRemoteNotification(userInfo, executeIfNonQuery: {
EVLog("Not a CloudKit Query notification.")
}, completed: {
EVLog("All notifications are processed")
}, completed: {
EVLog("All notifications are processed")
completionHandler(.NewData)
})
}



func applicationDidEnterBackground(application: UIApplication) {
// Just to make sure that all updates are written do the cache.
EVCloudData.publicDB.backupAllData()
Expand Down
47 changes: 45 additions & 2 deletions AppMessage/AppMessage/CloudKit/EVCloudKitDao.swift
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,28 @@ public class EVCloudKitDao {
})
}

/**
Save an array of items.
- parameter items: the items to save
- parameter completionHandler: The function that will be called with a CKRecord representation of the saved object
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
public func saveItems(items: [EVCloudKitDataObject], completionHandler: (records: [CKRecord]) -> Void, errorHandler:((error: NSError) -> Void)? = nil) {
let recordsToSave: [CKRecord] = items.map({EVCloudKitDao.publicDB.toCKRecord($0)})
let operation = CKModifyRecordsOperation(recordsToSave: recordsToSave, recordIDsToDelete: nil)
operation.atomic = false
operation.database = database
operation.modifyRecordsCompletionBlock = { (savedRecords: [CKRecord]?, deletedRecords: [CKRecordID]?, operationError: NSError?) -> Void in
self.handleCallback(self.nilNotAllowed(operationError, value: savedRecords), errorHandler: errorHandler, completionHandler: {
completionHandler(records: savedRecords!);
})
}
operation.start()
}


/**
Delete an Item for a recordId
Expand All @@ -526,6 +548,28 @@ public class EVCloudKitDao {
})
}


/**
Delete an array of items.
- parameter items: the items to save
- parameter completionHandler: The function that will be called with a CKRecord representation of the saved object
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
public func deleteItems(items: [EVCloudKitDataObject], completionHandler: (records: [CKRecord]) -> Void, errorHandler:((error: NSError) -> Void)? = nil) {
let recordsToDelete: [CKRecordID] = items.map({$0.recordID})
let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordsToDelete)
operation.atomic = false
operation.database = database
operation.modifyRecordsCompletionBlock = { (savedRecords: [CKRecord]?, deletedRecords: [CKRecordID]?, operationError: NSError?) -> Void in
self.handleCallback(self.nilNotAllowed(operationError, value: savedRecords), errorHandler: errorHandler, completionHandler: {
completionHandler(records: savedRecords!);
})
}
operation.start()
}

// ------------------------------------------------------------------------
// MARK: - Data methods - Query
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -804,8 +848,7 @@ public class EVCloudKitDao {
var converedUserInfo:[String:NSObject] = [String:NSObject]()
for (key, value) in userInfo {
if let setValue = value as? NSObject {
converedUserInfo[key as! String] = setValue

converedUserInfo[key as! String] = setValue
}
}

Expand Down
2 changes: 1 addition & 1 deletion EVCloudKitDao.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "EVCloudKitDao"
s.version = "2.15.0"
s.version = "2.16.0"
s.summary = "iOS: Simplified access to Apple’s CloudKit"

s.description = "Simplified access to Apple’s CloudKit using reflection and generics"
Expand Down

0 comments on commit b681cbd

Please sign in to comment.