Created
December 4, 2015 05:58
-
-
Save aryaxt/97374ab2e9a6374c7d9a to your computer and use it in GitHub Desktop.
A set of classes/extensions to abstract the parse logic and also using a Result enum to pass data around
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension PFCloud { | |
public class func fetchObjectFromCloud<T>(functionName: String, parameters: [NSObject: AnyObject] = [:], completion: NetworkResponse<T>->Void) { | |
PFCloud.callFunctionInBackground(functionName, withParameters: parameters) { result, error in | |
if let error = error { | |
completion(NetworkResponse.Failure(error)) | |
} | |
else { | |
completion(NetworkResponse.Success(result![0] as! T)) | |
} | |
} | |
} | |
public class func fetchObjectsFromCloud<T>(functionName: String, parameters: [NSObject: AnyObject] = [:], completion: NetworkResponse<[T]>->Void) { | |
PFCloud.callFunctionInBackground(functionName, withParameters: parameters) { result, error in | |
if let error = error { | |
completion(NetworkResponse.Failure(error)) | |
} | |
else { | |
let array = result as! [AnyObject] | |
completion(NetworkResponse.Success(array.map { $0 as! T } )) | |
} | |
} | |
} | |
} | |
public extension PFQuery { | |
public func findObjectsInBackgroundWithCompletion <T> (completion: NetworkResponse<[T]>->Void) { | |
findObjectsInBackgroundWithBlock { result, error in | |
if let error = error { | |
completion(NetworkResponse.Failure(error)) | |
} | |
else { | |
completion(NetworkResponse.Success(result!.map { $0 as! T })) | |
} | |
} | |
} | |
public func findObjectInBackgroundWithCompletion <T> (completion: NetworkResponse<T>->Void) { | |
findObjectsInBackgroundWithBlock { result, error in | |
if let error = error { | |
completion(NetworkResponse.Failure(error)) | |
} | |
else { | |
if result!.count == 1 { | |
completion(NetworkResponse.Success(result![0] as! T)) | |
} | |
else if result!.count == 0 { | |
completion(NetworkResponse.Failure(NetworkError.NoRecordsFound)) | |
} | |
else { | |
completion(NetworkResponse.Failure(NetworkError.MultipleRecordsFound)) | |
} | |
} | |
} | |
} | |
public func fetchObjectWithId<T>(id: String, completion: NetworkResponse<T>->Void) { | |
getObjectInBackgroundWithId(id) { object, error in | |
if let error = error { | |
completion(NetworkResponse.Failure(error)) | |
} | |
else { | |
completion(NetworkResponse.Success(object as! T)) | |
} | |
} | |
} | |
} | |
public enum NetworkError: ErrorType { | |
case MultipleRecordsFound | |
case NoRecordsFound | |
} | |
public enum NetworkResponse<T> { | |
case Success(T) | |
case Failure(ErrorType) | |
public var hasError: Bool { | |
switch self { | |
case .Success(_): | |
return false | |
case .Failure(_): | |
return true | |
} | |
} | |
public var result: T? { | |
if case .Success(let data) = self { | |
return data | |
} | |
return Optional.None | |
} | |
public var error: ErrorType? { | |
if case .Failure(let error) = self { | |
return error | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment