Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
Improved CKOperations
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyKJohnson committed Jul 30, 2016
1 parent 8804dcf commit fbf1aeb
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 89 deletions.
7 changes: 7 additions & 0 deletions Sources/Bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
//
//

protocol Bridgable {}

extension String: Bridgable {}
extension Int: Bridgable {}
extension Float: Bridgable {}
extension Double: Bridgable {}

#if !os(Linux)
import Foundation

Expand Down
24 changes: 21 additions & 3 deletions Sources/CKAccount.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public enum CKAccountType {
case server
}

public struct CKAccount: CKAccountInfoProvider {
public class CKAccount: CKAccountInfoProvider {

let accountType: CKAccountType

Expand All @@ -30,9 +30,9 @@ public struct CKAccount: CKAccountInfoProvider {

var iCloudAuthToken: String?

let cloudKitAuthToken: String
let cloudKitAuthToken: String?

init(type: CKAccountType, containerInfo: CKContainerInfo, cloudKitAuthToken: String) {
init(type: CKAccountType, containerInfo: CKContainerInfo, cloudKitAuthToken: String?) {
self.accountType = type
self.containerInfo = containerInfo
self.cloudKitAuthToken = cloudKitAuthToken
Expand All @@ -42,6 +42,24 @@ public struct CKAccount: CKAccountInfoProvider {
self.accountType = .primary
self.containerInfo = containerInfo
self.iCloudAuthToken = iCloudAuthToken
self.cloudKitAuthToken = cloudKitAuthToken
}

}

public class CKServerAccount: CKAccount {

let serverToServerAuth: CKServerToServerKeyAuth

init(containerInfo: CKContainerInfo, serverToServerAuth: CKServerToServerKeyAuth) {
self.serverToServerAuth = serverToServerAuth
super.init(type: .server, containerInfo: containerInfo, cloudKitAuthToken: nil)
}

convenience init(containerInfo: CKContainerInfo, keyID: String, privateKeyFile: String, passPhrase: String? = nil) {

let keyAuth = CKServerToServerKeyAuth(keyID: keyID, privateKeyFile: privateKeyFile, privateKeyPassPhrase: passPhrase)

self.init(containerInfo: containerInfo, serverToServerAuth: keyAuth)
}
}
2 changes: 1 addition & 1 deletion Sources/CKContainerInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct CKContainerInfo {
let containerID: String

var publicCloudDBURL: URL {
let baseURL = "\(CKServerInfo.path)/database/\(CKServerInfo.version)/\(containerID)/\(environment)/"
let baseURL = "\(CKServerInfo.path)/database/\(CKServerInfo.version)/\(containerID)/\(environment)/public"
return URL(string: baseURL)!
}

Expand Down
6 changes: 2 additions & 4 deletions Sources/CKDiscoverAllUserIdentitiesOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class CKDiscoverAllUserIdentitiesOperation : CKOperation {
self.discoverAllUserIdentitiesCompletionBlock?(error)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
finish()
}

override func performCKOperation() {
Expand Down Expand Up @@ -74,8 +73,7 @@ public class CKDiscoverAllUserIdentitiesOperation : CKOperation {
self.discoverAllUserIdentitiesCompletionBlock?(nil)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
self.finish(error: [])
}
}
}
6 changes: 2 additions & 4 deletions Sources/CKDiscoverUserIdentitiesOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public class CKDiscoverUserIdentitiesOperation : CKOperation {
self.discoverUserIdentitiesCompletionBlock?(error)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
self.finish(error: [])
}

override func performCKOperation() {
Expand Down Expand Up @@ -84,8 +83,7 @@ public class CKDiscoverUserIdentitiesOperation : CKOperation {
self.discoverUserIdentitiesCompletionBlock?(nil)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
self.finish(error: [])


}
Expand Down
6 changes: 2 additions & 4 deletions Sources/CKFetchRecordZonesOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public class CKFetchRecordZonesOperation : CKDatabaseOperation {
self.fetchRecordZonesCompletionBlock?(nil, error)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
finish(error: [])
}

override func performCKOperation() {
Expand Down Expand Up @@ -106,8 +105,7 @@ public class CKFetchRecordZonesOperation : CKDatabaseOperation {
self.fetchRecordZonesCompletionBlock?(self.recordZoneByZoneID, partialError)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
self.finish(error: [])

}
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/CKFetchRecordsOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public class CKFetchRecordsOperation: CKDatabaseOperation {
self.fetchRecordsCompletionBlock?(self.recordIDsToRecords, nil)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
self.finish(error: [])

}
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/CKModifyRecordZonesOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public class CKModifyRecordZonesOperation : CKDatabaseOperation {
self.modifyRecordZonesCompletionBlock?(Array(self.recordZonesByZoneIDs.values), self.recordZoneIDsToDelete, partialError)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
self.finish(error: [])


}
Expand Down
3 changes: 1 addition & 2 deletions Sources/CKModifyRecordsOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ public class CKModifyRecordsOperation: CKDatabaseOperation {
self.modifyRecordsCompletionBlock?(records, recordIDs, nil)

// Mark operation as complete
self.isExecuting = false
self.isFinished = true
self.finish(error: [])

}
}
Expand Down
Empty file modified Sources/CKModifyRecordsURLRequest.swift
100644 → 100755
Empty file.
86 changes: 66 additions & 20 deletions Sources/CKOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

import Foundation

enum CKOperationState: Int {
case initialized
case pending
case ready
case executing
case finished
}

public class CKOperation: Operation {

public var container: CKContainer?
Expand All @@ -20,7 +28,20 @@ public class CKOperation: Operation {

var urlSessionTask: URLSessionTask?

var request: CKURLRequest?

var childOperations: [CKOperation] = []

var operationID: String

var cloudKitMetrics: CKOperationMetrics?

weak var parentOperation: CKOperation?

private var state: CKOperationState = .initialized

override init() {
operationID = NSUUID().uuidString
super.init()
}

Expand All @@ -30,23 +51,41 @@ public class CKOperation: Operation {

public override func start() {

super.start()

// Check if operation is already cancelled
if isCancelled {
isFinished = true
state = .finished
return
}

// Perform CKOperation of superclass
performCKOperation()


// Send out KVO notifications for the executing
isExecuting = true
state = .executing

}

func execute() {

}

func addAndRun(childOperation: CKOperation) {
childOperations.append(childOperation)
childOperation.start()
}

func configure(request: CKURLRequest) {
// Configure Request
}

public override func main() {
super.main()
performCKOperation()

if !isCancelled {
performCKOperation()

} else {
finish()
}
}

public override func cancel() {
Expand All @@ -66,6 +105,10 @@ public class CKOperation: Operation {

func performCKOperation() {}

final func finish(error:[NSError] = []) {
state = .finished
}

#if os(Linux)

public var isFinished: Bool {
Expand Down Expand Up @@ -97,26 +140,29 @@ public class CKOperation: Operation {

#else
override public var isFinished: Bool {
get { return _isFinished }
set {
// willChangeValue(forKey: "isFinished")
_isFinished = newValue
// didChangeValue(forKey: "isFinished")
}
return state == .finished
}

override public var isExecuting: Bool {
get { return _isExecuting}
set {
// willChangeValue(forKey: "isExecuting")
_isExecuting = isExecuting
// didChangeValue(forKey: "isExecuting")
return state == .executing
}

override public var isReady: Bool {
switch state {
case .initialized:
return true
case .ready:
return super.isReady || isCancelled

}
default:
return false
} // MARK: State Management
}
override public var isConcurrent: Bool {
/*
public var isConcurrent: Bool {
get { return true }
}
*/
#endif
}

Expand Down
30 changes: 30 additions & 0 deletions Sources/CKOperationInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// CKOperationInfo.swift
// OpenCloudKit
//
// Created by Ben Johnson on 29/07/2016.
//
//

import Foundation

class CKOperationInfo {

}

class CKDatabaseOperationInfo: CKOperationInfo {
let databaseScope: CKDatabaseScope

init(databaseScope: CKDatabaseScope) {
self.databaseScope = databaseScope
super.init()
}

}

extension CKOperationInfo: CKCodable {
var dictionary: [String : AnyObject] {
return [:]
}
}

66 changes: 66 additions & 0 deletions Sources/CKPrettyError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// CKPrettyError.swift
// OpenCloudKit
//
// Created by Ben Johnson on 29/07/2016.
//
//

import Foundation

enum CKError {
case network(NSError)
case server([String: AnyObject])
case parse(NSError)

var error: NSError {
switch self {
case .network(let networkError):
return ckError(forNetworkError: networkError)
case .server(let dictionary):
return ckError(forServerResponseDictionary: dictionary)
case .parse(let parseError):
return NSError(domain: CKErrorDomain, code: CKErrorCode.InternalError.rawValue, userInfo: parseError.userInfo)
}
}

func ckError(forNetworkError networkError: NSError) -> NSError {
let userInfo = networkError.userInfo
let errorCode: CKErrorCode

switch networkError.code {
case NSURLErrorNotConnectedToInternet:
errorCode = .NetworkUnavailable
case NSURLErrorCannotFindHost, NSURLErrorCannotConnectToHost:
errorCode = .ServiceUnavailable
default:
errorCode = .NetworkFailure
}

let error = NSError(domain: CKErrorDomain, code: errorCode.rawValue, userInfo: userInfo)
return error
}

func ckError(forServerResponseDictionary dictionary: [String: AnyObject]) -> NSError {
if let recordFetchError = CKRecordFetchErrorDictionary(dictionary: dictionary) {

let errorCode = CKErrorCode.errorCode(serverError: recordFetchError.serverErrorCode)!

var userInfo = [:] as NSErrorUserInfoType

userInfo["redirectURL"] = recordFetchError.redirectURL
userInfo[NSLocalizedDescriptionKey] = recordFetchError.reason

userInfo[CKErrorRetryAfterKey] = recordFetchError.retryAfter
userInfo["uuid"] = recordFetchError.uuid

return NSError(domain: CKErrorDomain, code: errorCode.rawValue, userInfo: userInfo)

} else {

let userInfo = [:] as NSErrorUserInfoType
return NSError(domain: CKErrorDomain, code: CKErrorCode.InternalError.rawValue, userInfo: userInfo)
}
}
}

Loading

0 comments on commit fbf1aeb

Please sign in to comment.