-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swift Promise & REST API Delegate? #136
Comments
I found an example in UIAlertView+Promise.swift - but also read the warning in the docs against combining promises and delegates, so closing this. Would still welcome any examples or suggestions how to do this better. Thanks. |
You can do this, it's still hacky, but it is an alternative. import Foundation
import PromiseKit
class SalesforceObjectFetcher: NSObject {
// the type of the promise might be different, see fulfill usage to decide
let deferred = Promise<SalesforceObject>.defer()
init() {
deferred.promise.finally {
println(self) // here we force a retain on self inside the promise object
// it will be released once processed
}
}
func fetch (request: SFRestRequest) -> Promise<SalesforceObject> {
SFRestAPI.sharedInstance().send(request, delegate: self)
return deferred.promise
}
}
//MARK: SFRestDelegate compliance
extension SalesforceObjectFetcher: SFRestDelegate {
func request(request: SFRestRequest!, didFailLoadWithError error: NSError!) {
deferred.reject(error)
}
func request(request: SFRestRequest!, didLoadResponse dataResponse: AnyObject!) {
deferred.fulfill(dataResponse as SalesforceObject)
}
func requestDidCancelLoad(request: SFRestRequest!) {
let error = NSError(…) // make a code and domain to represent this error state
deferred.reject(error)
}
func requestDidTimeout(request: SFRestRequest!) {
let error = NSError(…)
deferred.reject(error)
}
} |
Thank you! |
@mxcl - I'm curious: why the println(self) to force a retain vs. PMKRetain( ) and PMKRelease( ) ? |
|
I don't see |
They were never public API. So they gone. But there's other ways to achieve the same. |
I'm struggling to create a Swift class that fetches data from the Salesforce REST API. The class conforms to Salesforce's REST API delegate protocol, and implements callback methods - so I need to fulfill or reject the promise from those methods, but don't know how. Thanks in advance for any help.
The text was updated successfully, but these errors were encountered: