Swift - How to properly use delegate

133 Views Asked by At

I am using Salesforce ios sdk for my mobile application and there is a method to execute SOQL queries. For eg.,

class SfdcController: NSObject, SFRestDelegate {   
    let request = SFRestAPI.sharedInstance().request(forQuery:"SELECT Name FROM User LIMIT 10");
    SFRestAPI.sharedInstance().send(request, delegate: self);

    func request(_ request: SFRestRequest, didLoadResponse jsonResponse: Any)
    {
        print(jsonResponse)
    }

    func request(_ request: SFRestRequest, didFailLoadWithError error: Error)
    {
       self.log(.debug, msg: "didFailLoadWithError: \(error)")
    }
}

Question I'll execute multiple SOQL queries with in the same class and there are different methods to handle the response. Now,

  • Is there a way to get the reference of the request inside the didLoadResponse? So, I can write switch statement to execute different functions.
  • If cannot reuse the delegate, do I need to create multiple delegate class to handle each and every response?

What is the better way to do it?

Update

request

SFRestRequest 0x1700cb130 
endpoint: /services/data 
method: GET 
path: /v39.0/sobjects/Event/describe 
queryParams: []

Other way to delegate is to create a separate class and define it.

SFRestAPI.sharedInstance().send(objectDescribe, delegate: testing());


class testing: NSObject, SFRestDelegate {

    func request(_ request: SFRestRequest, didLoadResponse jsonResponse: Any) {
        print(jsonResponse)
    }
}

But the problem with the above, I have to create a class every time when I want to execute a SOQL query.

Thanks

1

There are 1 best solutions below

0
ryantxr On

I have an idea for you. Create a separate delegate class and in that class put an identifier. Then let that separate class delegate back to your view controller.

// Create a protocol for it
class SFHandlerDelegate {
    func request(_ request: SFRestRequest, handlerId: Int, didLoadResponse jsonResponse: Any)
    func request(_ request: SFRestRequest, handlerId: Int, didFailLoadWithError error: Error)
}

Make a new delegate class

// this becomes the SF delegate
class SFHandler : SFRestDelegate {
    var id: Int = 0
    delegate: SFHandlerDelegate
    init(id: Int, delegate: SFHandlerDelegate) { 
        self.id = id 
        self.delegate = delegate
    }

    func request(_ request: SFRestRequest, didLoadResponse jsonResponse: Any)
    {
        delegate.request(request, handlerId: id, didLoadResponse: jsonResponse)
    }

    func request(_ request: SFRestRequest, didFailLoadWithError error: Error)
    {
        delegate.request(request, handlerId: id, didFailLoadWithError: error)
    }

}

Change the controller

class SfdcController: NSObject, SFHandlerDelegate {   
    let request = SFRestAPI.sharedInstance().request(forQuery:"SELECT Name FROM User LIMIT 10");
    let delegate = SFHandlerDelegate(self, 123) // use some handler id
    SFRestAPI.sharedInstance().send(request, delegate: delegate);

    func request(_ request: SFRestRequest, handlerId: Int, didLoadResponse jsonResponse: Any)
    {
        switch handlerId {
            case 123:
                doSomething()
            // whatever
        }
        print(jsonResponse)
    }

    func request(_ request: SFRestRequest, handlerId: Int, didFailLoadWithError error: Error)
    {
         switch handlerId {
            case 123:
                doSomething()
            // whatever
         }
         self.log(.debug, msg: "didFailLoadWithError: \(error)")
    }
}