Work with closure - Make code more general

There are two functions as shown below. Most of the functionality is the same for both. His idea is to get the webservice output from getResponse () [Helper helper call], parse and pass the information to the wrapped callback via getResult ().

 static func getAllDealers(dealerSearchServiceDomain: ARSDealerSearchServiceDomain, wrapperCallback:(getResult: () throws -> Void) -> Void) throws
    {
        try ARSInputValidator.validateZipCode(dealerSearchServiceDomain.zip)

        try ARSDealerConnection.getAllDealers(dealerSearchServiceDomain, helperCallback: { (getResponse) -> Void in

            do
            {
                let result = try getResponse()

                try ARSDealerParser.parseDealerSearchResponse(dealerSearchServiceDomain)

                wrapperCallback(getResult: { return })
            }
            catch
            {
                wrapperCallback(getResult: { throw error })
            }
        })
    }

    static func getDealerDetails(dealerDetailsServiceDomain: ARSDealerDetailsServiceDomain, wrapperCallback:(getResult: () throws -> Void) -> Void) throws
    {
        try ARSDealerConnection.getDealerDetails(dealerDetailsServiceDomain, helperCallback: { (getResponse) -> Void in

            do
            {
                let result = try getResponse()

                try ARSDealerParser.parseDealerDetailsResponse(dealerDetailsServiceDomain)

                wrapperCallback(getResult: { return })
            }
            catch
            {
                wrapperCallback(getResult: { throw error })
            }

        })
    }

I am trying to add a separate function for general functions like

static func parser(serviceCallDomain: ARSServiceCallDomain ,wrapperCallback:(getResult:() throws -> String) -> Void,  helperCallback:(getResponse:() throws -> String) -> Void) throws
    {
        helperCallback { (getResponse) -> Void in

But there is a compilation error, and I can not complete it. There are 15+ calls to web services, so the general indication I'm trying will be very useful.

The next step, I also need to pass the parseDealerSearchResponse () and parseDealerDetailsResponse () functions to the generic function.

I'm new to closing. Kindly help.

// EDIT - ADDING SAMPLE

Git - . Layer1.swift https://github.com/vivinjeganathan/ErrorHandling/tree/Closures-Refactor

0
1

, , , - , , , , - :

static func handleResponse(parser: Parser, validator: Validator, getResult: () throws -> AnyObject, completion: (getParsedResult: () throws -> AnyObject) -> Void)  {
    do
    {
        let result = try getResult()
        let parsedObject = try parser.parse(result)
        try validator.validate(parsedObject)
        completion(getParsedResult: { return parsedObject })
    }
    catch
    {
        completion(getParsedResult: { throw error })
    }
}

, , , , , ( View Controller), :

static func getAllDealers(dealerSearchServiceDomain: AnyObject, wrapperCallback:(getResult: () throws -> AnyObject) -> Void) throws {
    let validator = DealersValidator() // create real validator
    let parser = DealersParser() // create real parser

    try validator.validate(dealerSearchServiceDomain)

    try ARSDealerConnection.getAllDealers(dealerSearchServiceDomain, helperCallback: { (getResponse) -> Void in
        self.handleResponse(parser, validator: validator, getResult: getResponse, completion: wrapperCallback)
    })
}

handleResponse getAllDealers, , .

, , , , .

+1

Source: https://habr.com/ru/post/1621508/


All Articles