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

Commit

Permalink
Improved CKRecord Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyKJohnson committed Nov 30, 2016
1 parent 4a6ca51 commit e42a530
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
30 changes: 19 additions & 11 deletions Sources/CKRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,30 @@ extension CKRecord {
}
}

static func process(number: NSNumber, type: String) -> CKRecordValue {
switch(type) {
case "TIMESTAMP":
return NSDate(timeIntervalSince1970: number.doubleValue)
default:
return number
}
}

static func getValue(forRecordField field: [String: Any]) -> CKRecordValue? {
if let value = field[CKRecordFieldDictionary.value],
let type = field[CKRecordFieldDictionary.type] as? String {

switch value {
case let number as NSNumber:
switch(type) {
case "TIMESTAMP":
return NSDate(timeIntervalSince1970: number.doubleValue)
default:
return number
}
return process(number: number, type: type)

case let intValue as Int:
return NSNumber(value: intValue)
case let doubleValue as Int:
return NSNumber(value: doubleValue)
let number = NSNumber(value: intValue)
return process(number: number, type: type)

case let doubleValue as Double:
let number = NSNumber(value: doubleValue)
return process(number: number, type: type)

case let dictionary as [String: Any]:
switch type {
Expand All @@ -308,7 +316,7 @@ extension CKRecord {
case let string as String:
switch type {
case CKValueType.string:
return NSString(string: string)
return string
case CKValueType.data:
#if os(Linux)
return NSData(base64Encoded: string,
Expand All @@ -317,7 +325,7 @@ extension CKRecord {
return NSData(base64Encoded: string)
#endif
default:
return NSString(string: string)
return string
}

case let array as [Any]:
Expand Down
4 changes: 2 additions & 2 deletions Sources/CKURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class CKURLRequest: NSObject {
// urlComponents.queryItems = []
// if let accountInfo = accountInfoProvider {

let apiTokenItem = URLQueryItem(name: "ckAPIToken", value: accountInfo.cloudKitAuthToken)
// let apiTokenItem = URLQueryItem(name: "ckAPIToken", value: accountInfo.cloudKitAuthToken)
// urlComponents.queryItems?.append(apiTokenItem)

baseURL += "?ckAPIToken=\(accountInfo.cloudKitAuthToken ?? "")"
Expand Down Expand Up @@ -187,7 +187,7 @@ extension CKURLRequest: URLSessionDataDelegate {
CloudKit.debugPrint(jsonObject)

// Call completion block
if let error = CKErrorDictionary(dictionary: jsonObject) {
if let _ = CKErrorDictionary(dictionary: jsonObject) {
completionBlock?(.error(CKError.server(jsonObject)))
} else {
let result = CKURLRequestResult.success(jsonObject)
Expand Down

0 comments on commit e42a530

Please sign in to comment.