Last active
December 23, 2019 12:13
-
-
Save szehnder/84b0bd6f45a7f3f99306 to your computer and use it in GitHub Desktop.
Pattern for async callback between a view controller and a dataprovider singleton
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
typealias ServiceResponse = (NSDictionary?, NSError?) -> Void | |
class DataProvider: NSObject { | |
var client:AFHTTPRequestOperationManager? | |
let LOGIN_URL = "/api/v1/login" | |
class var sharedInstance:DataProvider { | |
struct Singleton { | |
static let instance = DataProvider() | |
} | |
return Singleton.instance | |
} | |
func setupClientWithBaseURLString(urlString:String) { | |
client = AFHTTPRequestOperationManager(baseURL: NSURL.URLWithString(urlString)) | |
client!.operationQueue = NSOperationQueue.mainQueue() | |
client!.responseSerializer = AFJSONResponseSerializer() | |
client!.requestSerializer = AFJSONRequestSerializer() | |
} | |
func loginWithEmailPassword(email:String, password:String, onCompletion: ServiceResponse) -> Void { | |
self.client!.POST(LOGIN_URL, parameters: ["email":email, "password":password] , success: {(operation:AFHTTPRequestOperation!, responseObject:AnyObject!) -> Void in | |
self.setupClientWithBaseURLString("http://somebaseurl.com") | |
let responseDict = responseObject as NSDictionary | |
// Note: This is where you would serialize the nsdictionary in the responseObject into one of your own model classes (or core data classes) | |
onCompletion(responseDict, nil) | |
}, failure: {(operation: AFHTTPRequestOperation!, error:NSError!) -> Void in | |
onCompletion(nil, error) | |
}) | |
} | |
} |
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
import UIKit | |
class MyViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
} | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
DataProvider.sharedInstance.loginWithEmailPassword(email:"[email protected]", password:"somepassword") { (responseObject:NSDictionary?, error:NSError?) in | |
if (error) { | |
println("Error logging you in!") | |
} else { | |
println("Do something in the view controller in response to successful login!") | |
} | |
} | |
} | |
} |
thanks....for this. I used to tell ppl learning Obj-C is like growing a third arm...Async Swift design seemed just as frustrating and painful...This really helps.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you define your client variable as non-Optional, then you don't need to add ! everytime