Skip to content

Commit

Permalink
refactor, delete fix, private named container fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Dec 5, 2015
1 parent db622e0 commit 1d4bb7e
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 155 deletions.
13 changes: 12 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@ before_install:
- if brew outdated | grep -qx xctool; then brew upgrade xctool; fi

script:
- xctool clean build -workspace AppMessage/AppMessage.xcworkspace -scheme AppMessage -sdk iphonesimulator9.1 ONLY_ACTIVE_ARCH=NO
- xctool clean build -workspace AppMessage/AppMessage.xcworkspace -scheme AppMessage -sdk iphonesimulator9.1 ONLY_ACTIVE_ARCH=NO

notifications:
slack: evcloudkitdao:MKq6Epb73ir879SC5A3sIVa0

notifications:
webhooks:
urls:
https://webhooks.gitter.im/e/95c83671324a980ca4b5
on_success: change # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: never # options: [always|never|change] default: always
6 changes: 5 additions & 1 deletion AppMessage/AppMessage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
7F2976581BB940730074C85A /* EVCloudData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FD41610199666F3003C2CE9 /* EVCloudData.swift */; };
7F2976591BB940730074C85A /* EVCloudKitDataObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F41BB841A2B2B92003FE1DD /* EVCloudKitDataObject.swift */; };
7F29765B1BB943660074C85A /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7F29765A1BB943660074C85A /* CloudKit.framework */; };
7F3C16501C1341ED0057B5E1 /* EVCloudKitEnums.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F3C164F1C1341ED0057B5E1 /* EVCloudKitEnums.swift */; };
7F41BB851A2B2B92003FE1DD /* EVCloudKitDataObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F41BB841A2B2B92003FE1DD /* EVCloudKitDataObject.swift */; };
7F434A2919628D490081F35C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F434A2819628D490081F35C /* AppDelegate.swift */; };
7F434A3019628D490081F35C /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7F434A2F19628D490081F35C /* Images.xcassets */; };
Expand Down Expand Up @@ -71,6 +72,7 @@
7F2976501BB93D6A0074C85A /* OSXTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSXTest.swift; sourceTree = "<group>"; };
7F2976521BB93D6A0074C85A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7F29765A1BB943660074C85A /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/CloudKit.framework; sourceTree = DEVELOPER_DIR; };
7F3C164F1C1341ED0057B5E1 /* EVCloudKitEnums.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVCloudKitEnums.swift; sourceTree = "<group>"; };
7F41BB841A2B2B92003FE1DD /* EVCloudKitDataObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVCloudKitDataObject.swift; sourceTree = "<group>"; };
7F434A2319628D490081F35C /* AppMessage.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AppMessage.app; sourceTree = BUILT_PRODUCTS_DIR; };
7F434A2719628D490081F35C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -237,10 +239,11 @@
7F434A4519628E2D0081F35C /* CloudKit */ = {
isa = PBXGroup;
children = (
7F07C54C1AF0C127000DF1F3 /* EVglobal.swift */,
7F434A4619628EAA0081F35C /* EVCloudKitDao.swift */,
7FD41610199666F3003C2CE9 /* EVCloudData.swift */,
7F41BB841A2B2B92003FE1DD /* EVCloudKitDataObject.swift */,
7F07C54C1AF0C127000DF1F3 /* EVglobal.swift */,
7F3C164F1C1341ED0057B5E1 /* EVCloudKitEnums.swift */,
);
path = CloudKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -652,6 +655,7 @@
7FC8B3B119E0600900C146AE /* MenuViewController.swift in Sources */,
7F0992E51AEA3BAD00522AE6 /* SSASideMenu.swift in Sources */,
7F434A2919628D490081F35C /* AppDelegate.swift in Sources */,
7F3C16501C1341ED0057B5E1 /* EVCloudKitEnums.swift in Sources */,
7FC46F8C19814C17000A875F /* LeftMenuViewController.swift in Sources */,
7F434A4719628EAA0081F35C /* EVCloudKitDao.swift in Sources */,
7FC46F9019824285000A875F /* HomeViewController.swift in Sources */,
Expand Down
139 changes: 19 additions & 120 deletions AppMessage/AppMessage/CloudKit/EVCloudData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,113 +9,6 @@ import Foundation
import CloudKit
import EVReflection

/**
The enum for specifying the caching strategy for the data
*/
public enum CachingStrategy {
/**
Do not cache this
*/
case None,
/**
Always write changes to the cache immediately
*/
Direct,
/**
Only write to the cache once every .. minutes when there are changes (initial query result will always be written directly)
*/
Every(minute:Int)
}

/**
The enum for getting the notification key to subscribe to when observing changes
*/
public enum DataChangeNotificationType {
/**
Data retrieval is progressing/finished
*/
case Completed,
/**
New item has been inserted
*/
Inserted,
/**
Existing item has been updated
*/
Updated,
/**
Notification of any data modification (completion, inserted, updated or deleted)
*/
DataChanged,
/**
Existing item has been deleted
*/
Deleted,
/**
An error occurred while attempting a data operation
*/
Error
}

/**
The enum for determining the current state of data retrieval in the Completion handler and/or NSNotificationManager push notification
*/
public enum CompletionStatus: Int {
/**
The results were returned from the local cache
*/
case FromCache,
/**
The requested data wasn't found in the local cache. It will be requested from iCloud
*/
Retrieving,
/**
Some data was received from iCloud, but more results are available if wanted (return true to request more results)
*/
PartialResult,
/**
All available data has been successfully retrieved from iCloud
*/
FinalResult
}

/**
Strange enough by default Swift does not implement the Equality operator for enums. So we just made one ourselves.
- parameter leftPart: The CachingStrategy value at the left of the equality operator.
- parameter rightPart: The CachingStrategy value at the right of the equality operator.
*/
func ==(leftPart: CachingStrategy, rightPart: CachingStrategy) -> Bool {
switch(leftPart) {
case .None:
switch(rightPart) {
case .None: return true
default: return false
}
case .Direct:
switch(rightPart) {
case .Direct: return true
default: return false
}
case .Every(let minutea):
switch(rightPart) {
case .Every(let minuteb): return minutea == minuteb
default: return false
}
}
}


/**
Strange enough by default Swift does not implement the not Equality operator for enums. So we just made one ourselves.
- parameter leftPart: The CachingStrategy value at the left of the equality operator.
- parameter rightPart: The CachingStrategy value at the right of the equality operator.
*/
func !=(leftPart: CachingStrategy, rightPart: CachingStrategy) -> Bool {
return !(leftPart == rightPart)
}


/**
Wrapper class for being able to use a class instance Dictionary.
Expand Down Expand Up @@ -240,7 +133,7 @@ public class EVCloudData: NSObject {
// ------------------------------------------------------------------------

/**
The EVCloudKitDao instance that will be used
The EVCloudKitDao instance that will be used. Defaults to the publicDB
*/
public var dao = EVCloudKitDao.publicDB;
/**
Expand Down Expand Up @@ -325,7 +218,7 @@ public class EVCloudData: NSObject {
- parameter filterId: The filter id for the data that was received from CloudKit
*/
private func dataIsUpdated(filterId: String) {
cachingChangesCount[filterId] = cachingChangesCount[filterId]! + 1
cachingChangesCount[filterId] = (cachingChangesCount[filterId] ?? 0) + 1
backupDataWithStrategyTest(filterId)
}

Expand Down Expand Up @@ -500,7 +393,8 @@ public class EVCloudData: NSObject {
// Verify notifications are wanted
if postNotifications[filterId] != nil {
// Post requested notification
NSNotificationCenter.defaultCenter().postNotificationName(EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Completed), object: self, userInfo: ["filterId": filterId, "results":results, "status": status.rawValue])
let notificationId = EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Completed)
NSNotificationCenter.defaultCenter().postNotificationName(notificationId, object: self, userInfo: ["filterId": filterId, "results":results, "status": status.rawValue])
// Post universal "data changed" notification
postDataChangeNotification(filterId)
}
Expand All @@ -516,7 +410,8 @@ public class EVCloudData: NSObject {
// Verify notifications are wanted
if postNotifications[filterId] != nil {
// Post requested notification
NSNotificationCenter.defaultCenter().postNotificationName(EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Inserted), object: self, userInfo: ["filterId": filterId, "item":item])
let notificationId = EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Inserted)
NSNotificationCenter.defaultCenter().postNotificationName(notificationId, object: self, userInfo: ["filterId": filterId, "item":item])
// Post universal "data changed" notification
postDataChangeNotification(filterId)
}
Expand All @@ -533,7 +428,8 @@ public class EVCloudData: NSObject {
// Verify notifications are wanted
if postNotifications[filterId] != nil {
// Post requested notification
NSNotificationCenter.defaultCenter().postNotificationName(EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Updated), object: self, userInfo: ["filterId": filterId, "item":item, "dataIndex":dataIndex])
let notificationId = EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Updated)
NSNotificationCenter.defaultCenter().postNotificationName(notificationId, object: self, userInfo: ["filterId": filterId, "item":item, "dataIndex":dataIndex])
// Post universal "data changed" notification
postDataChangeNotification(filterId)
}
Expand All @@ -548,7 +444,8 @@ public class EVCloudData: NSObject {
// Verify notifications are wanted
if postNotifications[filterId] != nil {
// Post requested notification
NSNotificationCenter.defaultCenter().postNotificationName(EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.DataChanged), object: self, userInfo: ["filterId": filterId])
let notificationId = EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.DataChanged)
NSNotificationCenter.defaultCenter().postNotificationName(notificationId, object: self, userInfo: ["filterId": filterId])
}
}

Expand All @@ -563,7 +460,8 @@ public class EVCloudData: NSObject {
// Verify notifications are wanted
if postNotifications[filterId] != nil {
// Post requested notification
NSNotificationCenter.defaultCenter().postNotificationName(EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Deleted), object: self, userInfo: ["filterId": filterId, "recordId":recordId, "dataIndex":dataIndex])
let notificationId = EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Deleted)
NSNotificationCenter.defaultCenter().postNotificationName(notificationId, object: self, userInfo: ["filterId": filterId, "recordId":recordId, "dataIndex":dataIndex])
// Post universal "data changed" notification
postDataChangeNotification(filterId)
}
Expand All @@ -579,7 +477,8 @@ public class EVCloudData: NSObject {
// Verify notifications are wanted
if postNotifications[filterId] != nil {
// Post requested notification
NSNotificationCenter.defaultCenter().postNotificationName(EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Error), object: self, userInfo: ["filterId": filterId, "error":error])
let notificationId = EVCloudData.getNotificationCenterId(filterId, changeType: DataChangeNotificationType.Error)
NSNotificationCenter.defaultCenter().postNotificationName(notificationId, object: self, userInfo: ["filterId": filterId, "error":error])
// No universal "data changed" notification on errors
}
}
Expand Down Expand Up @@ -650,18 +549,18 @@ public class EVCloudData: NSObject {
*/
private func deleteObject(recordId: String) {
for (filter, _) in self.data {
let itemID: Int? = data[filter]!.EVindexOf {item in return item.recordID.recordName == recordId}
if itemID != nil {
data[filter]!.removeAtIndex(itemID!)
if let itemID: Int = data[filter]?.EVindexOf({item in return item.recordID.recordName == recordId}) {
data[filter]!.removeAtIndex(itemID)
NSOperationQueue.mainQueue().addOperationWithBlock {
(self.deletedHandlers[filter]!)(recordId: recordId, dataIndex:itemID!)
(self.deletedHandlers[filter]!)(recordId: recordId, dataIndex:itemID)
(self.dataChangedHandlers[filter]!)()
self.postDataDeletedNotification(filter, recordId: recordId, dataIndex: itemID!)
self.postDataDeletedNotification(filter, recordId: recordId, dataIndex: itemID)
}
dataIsUpdated(filter)
}
}
}


// ------------------------------------------------------------------------
// MARK: - Data methods - CRUD
Expand Down
37 changes: 6 additions & 31 deletions AppMessage/AppMessage/CloudKit/EVCloudKitDao.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,6 @@
import CloudKit
import EVReflection

/**
Wrapper class for being able to use a class instance Dictionary
*/
private class DaoContainerWrapper {
/**
Wrapping the public containers
*/
var publicContainers : Dictionary<String,EVCloudKitDao> = Dictionary<String,EVCloudKitDao>()
/**
Wrapping the private containers
*/
var privateContainers : Dictionary<String,EVCloudKitDao> = Dictionary<String,EVCloudKitDao>()
}

/**
The functional statuses for a CloudKit error
*/
public enum HandleCloudKitErrorAs {
case Success,
Retry(afterSeconds:Double),
RecoverableError,
Fail
}

public enum InstanceType {
case IsPrivate,
IsPublic
}

/**
Class for simplified access to Apple's CloudKit data where you still have full control
Expand Down Expand Up @@ -123,8 +95,11 @@ public class EVCloudKitDao {
if let containerInstance = containerWrapperInstance.privateContainers[containterIdentifier] {
return containerInstance
}
containerWrapperInstance.privateContainers[containterIdentifier] = EVCloudKitDao(containerIdentifier: containterIdentifier)
return containerWrapperInstance.privateContainers[containterIdentifier]!
let dao = EVCloudKitDao(containerIdentifier: containterIdentifier)
dao.isType = .IsPrivate
dao.database = dao.container.privateCloudDatabase
containerWrapperInstance.privateContainers[containterIdentifier] = dao
return dao
}


Expand Down Expand Up @@ -656,7 +631,7 @@ public class EVCloudKitDao {
let createSubscription = { () -> () in
let subscription = CKSubscription(recordType: recordType, predicate: predicate, subscriptionID:key, options: [.FiresOnRecordCreation, .FiresOnRecordUpdate, .FiresOnRecordDeletion])
subscription.notificationInfo = CKNotificationInfo()
// tvOS does not have visible remote notifications. This property is not available.
#if os(tvOS)
#else
subscription.notificationInfo!.shouldSendContentAvailable = true
Expand Down
Loading

0 comments on commit 1d4bb7e

Please sign in to comment.