Skip to content
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

Closed
frizzy75 opened this issue Jan 25, 2015 · 7 comments
Closed

Swift Promise & REST API Delegate? #136

frizzy75 opened this issue Jan 25, 2015 · 7 comments

Comments

@frizzy75
Copy link

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.

import Foundation
import PromiseKit

class SalesforceObjectFetcher: NSObject, SFRestDelegate {

    var promise: Promise<SalesforceObject>!

    func fetch (request: SFRestRequest) -> Promise<SalesforceObject> {
        SFRestAPI.sharedInstance().send(request, delegate: self)
        promise = Promise<SalesforceObject>({ (fulfill, reject) -> Void in
            //TODO
        })
        return promise
    }

}

//MARK: SFRestDelegate compliance
extension SalesforceObjectFetcher {

    func request(request: SFRestRequest!, didFailLoadWithError error: NSError!) {
        //TODO: need to reject promise from here...
    }

    func request(request: SFRestRequest!, didLoadResponse dataResponse: AnyObject!) {
        //TODO: need to fulfill promise from here after converting NSDictionary response to model objects...
    }

    func requestDidCancelLoad(request: SFRestRequest!) {
        //TODO: need to reject promise from here...
    }

    func requestDidTimeout(request: SFRestRequest!) {
        //TODO: need to reject promise from here...
    }
}
@frizzy75 frizzy75 changed the title Swift Promise for Salesforce REST API Delegate? Swift Promise for REST API Delegate? Jan 26, 2015
@frizzy75 frizzy75 changed the title Swift Promise for REST API Delegate? Swift Promise & REST API Delegate? Jan 26, 2015
@frizzy75
Copy link
Author

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.

@mxcl
Copy link
Owner

mxcl commented Jan 30, 2015

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)
    }
}

@frizzy75
Copy link
Author

Thank you!

@frizzy75
Copy link
Author

frizzy75 commented Feb 5, 2015

@mxcl - I'm curious: why the println(self) to force a retain vs. PMKRetain( ) and PMKRelease( ) ?
Thanks

@mxcl
Copy link
Owner

mxcl commented Feb 5, 2015

PMKRetain is better, I didn't suggest it because the suggestion is easier to understand, but if you understand the macros, use them!

@chkpnt
Copy link

chkpnt commented Mar 24, 2017

I don't see PMKRetain / PMKRelease in the current release. Was it renamed at some time?

@mxcl
Copy link
Owner

mxcl commented Mar 25, 2017

They were never public API. So they gone. But there's other ways to achieve the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants