Skip to content

Commit

Permalink
return CKQueryOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Mar 29, 2016
1 parent b30c561 commit f54cd47
Show file tree
Hide file tree
Showing 19 changed files with 1,386 additions and 30 deletions.
1 change: 1 addition & 0 deletions AppMessage/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

938 changes: 938 additions & 0 deletions AppMessage/.idea/AppMessage.iml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions AppMessage/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions AppMessage/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions AppMessage/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions AppMessage/.idea/runConfigurations/AppMessage.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions AppMessage/.idea/runConfigurations/OSXTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions AppMessage/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

350 changes: 350 additions & 0 deletions AppMessage/.idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions AppMessage/.idea/xcode.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions AppMessage/AppMessage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
7F61CA3A19829CC7005EF254 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 7F61CA3719829CC7005EF254 /* [email protected] */; };
7F61CA3B19829CC7005EF254 /* MessageIncoming.aiff in Resources */ = {isa = PBXBuildFile; fileRef = 7F61CA3819829CC7005EF254 /* MessageIncoming.aiff */; };
7F61CA3C19829CC7005EF254 /* MessageOutgoing.aiff in Resources */ = {isa = PBXBuildFile; fileRef = 7F61CA3919829CC7005EF254 /* MessageOutgoing.aiff */; };
7F6A7CE51C6A765E00B1DC88 /* EVSort.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F9E680F1C675F9E00F0F4D3 /* EVSort.swift */; };
7F6A7CE61C6A765F00B1DC88 /* EVSort.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F9E680F1C675F9E00F0F4D3 /* EVSort.swift */; };
7F76554C197FF0FE008AD0D5 /* Asset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F76554B197FF0FE008AD0D5 /* Asset.swift */; };
7F7C03621A382C3C00054ED7 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 7F7C03611A382C3C00054ED7 /* Localizable.strings */; };
7F9E68101C675F9E00F0F4D3 /* EVSort.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F9E680F1C675F9E00F0F4D3 /* EVSort.swift */; };
Expand Down Expand Up @@ -638,6 +640,7 @@
files = (
7F4CA2C21C4121C600AEE29D /* EVCloudKitEnums.swift in Sources */,
7F2976581BB940730074C85A /* EVCloudData.swift in Sources */,
7F6A7CE51C6A765E00B1DC88 /* EVSort.swift in Sources */,
7F2976511BB93D6A0074C85A /* OSXTest.swift in Sources */,
7F2976561BB940730074C85A /* EVglobal.swift in Sources */,
7F2976571BB940730074C85A /* EVCloudKitDao.swift in Sources */,
Expand Down Expand Up @@ -681,6 +684,7 @@
files = (
7F4CA2C11C4121C500AEE29D /* EVCloudKitEnums.swift in Sources */,
7F4A37051BDD92D600A0F73F /* EVglobal.swift in Sources */,
7F6A7CE61C6A765F00B1DC88 /* EVSort.swift in Sources */,
7F4A37061BDD92D600A0F73F /* EVCloudKitDao.swift in Sources */,
7F4A37071BDD92D600A0F73F /* EVCloudData.swift in Sources */,
7F4A37081BDD92D600A0F73F /* EVCloudKitDataObject.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions AppMessage/AppMessage/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// 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!
// 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()])
//EVCloudKitDao.publicDB.createRecordTypes([Message(), Asset(), News(), Invoice()])


// During development you will probably play around with subscriptins.
Expand All @@ -43,7 +43,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
})
}

#if os(tvOS)
// #if os(tvOS)
//This will only be called when your app is active. So this is what you should use on tvOS
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
EVLog("Push received")
Expand All @@ -53,7 +53,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
EVLog("All notifications are processed")
})
}
#else
// #else
// Process al notifications even if we are in the background. tvOS will not have this event
// Make sure you enable background notifications in the app settings. (entitlements: pushnotifications and backgrounds modes - notifications plus background fetch)
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
Expand All @@ -65,7 +65,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
completionHandler(.NewData)
})
}
#endif
// #endif

func applicationDidEnterBackground(application: UIApplication) {
// Just to make sure that all updates are written do the cache.
Expand Down
13 changes: 7 additions & 6 deletions AppMessage/AppMessage/CloudKit/EVCloudData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class EVCloudData: NSObject {
*/
override init() {
super.init()
NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: Selector("backupAllData"), userInfo: nil, repeats: true)
NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(EVCloudData.backupAllData), userInfo: nil, repeats: true)
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -296,12 +296,13 @@ public class EVCloudData: NSObject {
- returns: ?
*/
public class func getNotificationCenterId(filterId: String, var changeType: DataChangeNotificationType? = nil) -> String {
public class func getNotificationCenterId(filterId: String, changeType: DataChangeNotificationType? = nil) -> String {
if changeType == nil {
changeType = DataChangeNotificationType.DataChanged
return "NL.EVICT.CloudKit.\(filterId).\(DataChangeNotificationType.DataChanged)"
} else {
return "NL.EVICT.CloudKit.\(filterId).\(changeType!)"
}

return "NL.EVICT.CloudKit.\(filterId).\(changeType!)"
}

/**
Expand Down Expand Up @@ -614,7 +615,7 @@ public class EVCloudData: NSObject {
deletedHandler:((recordId: String, dataIndex: Int) -> Void)? = nil,
dataChangedHandler:(() -> Void)? = nil,
errorHandler:((error: NSError) -> Void)? = nil
) -> Void {
) -> CKQueryOperation {
// Set the post notifications flag for this filter ID before checking the cache
if postNotifications != nil && postNotifications! {
self.postNotifications[filterId] = postNotifications!
Expand Down Expand Up @@ -690,7 +691,7 @@ public class EVCloudData: NSObject {

dao.subscribe(type, predicate:predicate, filterId: filterId, configureNotificationInfo:configureNotificationInfo ,errorHandler: errorHandler)

dao.query(type, predicate: predicate, orderBy: orderBy, completionHandler: { results, isFinished in
return dao.query(type, predicate: predicate, orderBy: orderBy, completionHandler: { results, isFinished in
if self.data[filterId] != nil && self.data[filterId]! == results && self.data[filterId]!.count > 0 {
return false // Result was already returned from cache
}
Expand Down
38 changes: 24 additions & 14 deletions AppMessage/AppMessage/CloudKit/EVCloudKitDao.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,13 @@ public class EVCloudKitDao {
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
internal func queryRecords<T:EVCloudKitDataObject>(type:T, query: CKQuery, completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) {
internal func queryRecords<T:EVCloudKitDataObject>(type:T, query: CKQuery, completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) -> CKQueryOperation {
if !(query.sortDescriptors != nil) {
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
}
let operation = CKQueryOperation(query: query)
operation.qualityOfService = .UserInitiated
operation.queuePriority = .VeryHigh
var results = [T]()
operation.recordFetchedBlock = { record in
if let parsed = self.fromCKRecord(record) as? T {
Expand All @@ -329,6 +330,7 @@ public class EVCloudKitDao {
}
operation.resultsLimit = CKQueryOperationMaximumResults;
database.addOperation(operation)
return operation
}


Expand All @@ -341,7 +343,7 @@ public class EVCloudKitDao {
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
private func queryRecords<T:EVCloudKitDataObject>(cursor: CKQueryCursor, continueWithResults:[T], completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) {
private func queryRecords<T:EVCloudKitDataObject>(cursor: CKQueryCursor, continueWithResults:[T], completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) -> CKQueryOperation {
var results = continueWithResults
let operation = CKQueryOperation(cursor: cursor)
operation.qualityOfService = .UserInitiated
Expand All @@ -362,6 +364,7 @@ public class EVCloudKitDao {
}
operation.resultsLimit = CKQueryOperationMaximumResults;
database.addOperation(operation)
return operation
}


Expand Down Expand Up @@ -595,7 +598,7 @@ public class EVCloudKitDao {
- 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) {
public func saveItems(items: [EVCloudKitDataObject], completionHandler: (records: [CKRecord]) -> Void, errorHandler:((error: NSError) -> Void)? = nil) -> CKModifyRecordsOperation {
let recordsToSave: [CKRecord] = items.map({self.toCKRecord($0)})
let operation = CKModifyRecordsOperation(recordsToSave: recordsToSave, recordIDsToDelete: nil)
operation.atomic = false
Expand All @@ -606,6 +609,7 @@ public class EVCloudKitDao {
})
}
operation.start()
return operation
}


Expand Down Expand Up @@ -634,7 +638,7 @@ public class EVCloudKitDao {
- 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) {
public func deleteItems(items: [EVCloudKitDataObject], completionHandler: (records: [CKRecord]) -> Void, errorHandler:((error: NSError) -> Void)? = nil) -> CKModifyRecordsOperation {
let recordsToDelete: [CKRecordID] = items.map({$0.recordID})
let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordsToDelete)
operation.atomic = false
Expand All @@ -645,8 +649,10 @@ public class EVCloudKitDao {
})
}
operation.start()
return operation
}


// ------------------------------------------------------------------------
// MARK: - Data methods - Query
// ------------------------------------------------------------------------
Expand All @@ -659,11 +665,11 @@ public class EVCloudKitDao {
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
public func query<T:EVCloudKitDataObject>(type:T, orderBy: OrderBy = Descending(field: "creationDate"), completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) {
public func query<T:EVCloudKitDataObject>(type:T, orderBy: OrderBy = Descending(field: "creationDate"), completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) -> CKQueryOperation {
let recordType = EVReflection.swiftStringFromClass(type)
let query = CKQuery(recordType: recordType, predicate: NSPredicate(value: true))
query.sortDescriptors = orderBy.sortDescriptors()
queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
return queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
}

/**
Expand All @@ -676,15 +682,19 @@ public class EVCloudKitDao {
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
public func query<T:EVCloudKitDataObject>(type:T, referenceRecordName: String, referenceField: String, orderBy: OrderBy = Descending(field: "creationDate"), completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) {
public func query<T:EVCloudKitDataObject>(type:T, referenceRecordName: String, referenceField: String, orderBy: OrderBy = Descending(field: "creationDate"), completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) -> CKQueryOperation {
let recordType = EVReflection.swiftStringFromClass(type)
let parentId = CKRecordID(recordName: referenceRecordName)
let parent = CKReference(recordID: parentId, action: CKReferenceAction.None)
let query = CKQuery(recordType: recordType, predicate: NSPredicate(format: "%K == %@", referenceField ,parent))
query.sortDescriptors = orderBy.sortDescriptors()
queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
return queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
}





/**
Query a recordType with a predicate
Expand All @@ -694,11 +704,11 @@ public class EVCloudKitDao {
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
public func query<T:EVCloudKitDataObject>(type:T, predicate: NSPredicate, orderBy: OrderBy = Descending(field: "creationDate"), completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil){
public func query<T:EVCloudKitDataObject>(type:T, predicate: NSPredicate, orderBy: OrderBy = Descending(field: "creationDate"), completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) -> CKQueryOperation{
let recordType = EVReflection.swiftStringFromClass(type)
let query: CKQuery = CKQuery(recordType: recordType, predicate: predicate)
query.sortDescriptors = orderBy.sortDescriptors()
queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
return queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
}

/**
Expand All @@ -710,11 +720,11 @@ public class EVCloudKitDao {
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
public func query<T:EVCloudKitDataObject>(type:T, tokens: String, orderBy: OrderBy = Descending(field: "creationDate"), completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) {
public func query<T:EVCloudKitDataObject>(type:T, tokens: String, orderBy: OrderBy = Descending(field: "creationDate"), completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) -> CKQueryOperation {
let recordType = EVReflection.swiftStringFromClass(type)
let query = CKQuery(recordType: recordType, predicate: NSPredicate(format: "allTokens TOKENMATCHES[cdl] %@", tokens))
query.sortDescriptors = orderBy.sortDescriptors()
queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
return queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
}

/**
Expand All @@ -729,13 +739,13 @@ public class EVCloudKitDao {
- parameter errorHandler: The function that will be called when there was an error
:return: No return value
*/
public func query<T: EVCloudKitDataObject>(type: T, fieldname: String, latitude: Double, longitude: Double, distance: Int ,completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) {
public func query<T: EVCloudKitDataObject>(type: T, fieldname: String, latitude: Double, longitude: Double, distance: Int ,completionHandler: (results: [T], isFinished: Bool) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) -> CKQueryOperation{
let recordType: String = EVReflection.swiftStringFromClass(type)
let location: CLLocation = CLLocation(latitude: CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude))
let predecate: NSPredicate = NSPredicate(format: "distanceToLocation:fromLocation:(%K, %@) < %@", [fieldname, location, distance])
let query = CKQuery(recordType:recordType, predicate:predecate)
query.sortDescriptors = [CKLocationSortDescriptor(key: fieldname, relativeLocation: location)]
queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
return queryRecords(type, query:query, completionHandler: completionHandler, errorHandler: errorHandler)
}

// ------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit f54cd47

Please sign in to comment.