Swift Synchronous API calls











up vote
-3
down vote

favorite












I have to make three API calls to server one after another(synchronous),second API has to be called only if the first one is success. If first API fails then I have to to stop the flow and show another view controller with retry and cancel. What should I use and How should I do it?










share|improve this question







New contributor




Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use Operation Queue and manage the dependency using OperationQueue
    – HPM
    yesterday








  • 3




    There are more than one solutions to your problem. Here are some keywords that you should be looking for: NSOperation, GCD, futures & promises, async/await pattern. The way that Apple handles this is by operations that depend on other operations. Use of Promises is quite popular for these tasks and finally async/await maybe the cleanest (cognitive load wise) approach to promises (and what I would suggest you to check first)
    – Alladinian
    yesterday

















up vote
-3
down vote

favorite












I have to make three API calls to server one after another(synchronous),second API has to be called only if the first one is success. If first API fails then I have to to stop the flow and show another view controller with retry and cancel. What should I use and How should I do it?










share|improve this question







New contributor




Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use Operation Queue and manage the dependency using OperationQueue
    – HPM
    yesterday








  • 3




    There are more than one solutions to your problem. Here are some keywords that you should be looking for: NSOperation, GCD, futures & promises, async/await pattern. The way that Apple handles this is by operations that depend on other operations. Use of Promises is quite popular for these tasks and finally async/await maybe the cleanest (cognitive load wise) approach to promises (and what I would suggest you to check first)
    – Alladinian
    yesterday















up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I have to make three API calls to server one after another(synchronous),second API has to be called only if the first one is success. If first API fails then I have to to stop the flow and show another view controller with retry and cancel. What should I use and How should I do it?










share|improve this question







New contributor




Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have to make three API calls to server one after another(synchronous),second API has to be called only if the first one is success. If first API fails then I have to to stop the flow and show another view controller with retry and cancel. What should I use and How should I do it?







objective-c swift grand-central-dispatch






share|improve this question







New contributor




Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Revathi Bhavanam

11




11




New contributor




Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Revathi Bhavanam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Use Operation Queue and manage the dependency using OperationQueue
    – HPM
    yesterday








  • 3




    There are more than one solutions to your problem. Here are some keywords that you should be looking for: NSOperation, GCD, futures & promises, async/await pattern. The way that Apple handles this is by operations that depend on other operations. Use of Promises is quite popular for these tasks and finally async/await maybe the cleanest (cognitive load wise) approach to promises (and what I would suggest you to check first)
    – Alladinian
    yesterday




















  • Use Operation Queue and manage the dependency using OperationQueue
    – HPM
    yesterday








  • 3




    There are more than one solutions to your problem. Here are some keywords that you should be looking for: NSOperation, GCD, futures & promises, async/await pattern. The way that Apple handles this is by operations that depend on other operations. Use of Promises is quite popular for these tasks and finally async/await maybe the cleanest (cognitive load wise) approach to promises (and what I would suggest you to check first)
    – Alladinian
    yesterday


















Use Operation Queue and manage the dependency using OperationQueue
– HPM
yesterday






Use Operation Queue and manage the dependency using OperationQueue
– HPM
yesterday






3




3




There are more than one solutions to your problem. Here are some keywords that you should be looking for: NSOperation, GCD, futures & promises, async/await pattern. The way that Apple handles this is by operations that depend on other operations. Use of Promises is quite popular for these tasks and finally async/await maybe the cleanest (cognitive load wise) approach to promises (and what I would suggest you to check first)
– Alladinian
yesterday






There are more than one solutions to your problem. Here are some keywords that you should be looking for: NSOperation, GCD, futures & promises, async/await pattern. The way that Apple handles this is by operations that depend on other operations. Use of Promises is quite popular for these tasks and finally async/await maybe the cleanest (cognitive load wise) approach to promises (and what I would suggest you to check first)
– Alladinian
yesterday














1 Answer
1






active

oldest

votes

















up vote
0
down vote













Using TRVSURLSessionOperation would help you, here is how to do it:



let queue = OperationQueue()
let session = URLSession(configuration: URLSessionConfiguration.default)
let firstOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "first url")!)) { (data, response, error) in
// Your completion logic
}
let secondOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "second url")!)) { (data, response, error) in
// Your completion logic
}
secondOperation?.addDependency(firstOperation!)
queue.addOperations([firstOperation!, secondOperation!], waitUntilFinished: false)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Revathi Bhavanam is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372229%2fswift-synchronous-api-calls%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Using TRVSURLSessionOperation would help you, here is how to do it:



    let queue = OperationQueue()
    let session = URLSession(configuration: URLSessionConfiguration.default)
    let firstOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "first url")!)) { (data, response, error) in
    // Your completion logic
    }
    let secondOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "second url")!)) { (data, response, error) in
    // Your completion logic
    }
    secondOperation?.addDependency(firstOperation!)
    queue.addOperations([firstOperation!, secondOperation!], waitUntilFinished: false)





    share|improve this answer



























      up vote
      0
      down vote













      Using TRVSURLSessionOperation would help you, here is how to do it:



      let queue = OperationQueue()
      let session = URLSession(configuration: URLSessionConfiguration.default)
      let firstOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "first url")!)) { (data, response, error) in
      // Your completion logic
      }
      let secondOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "second url")!)) { (data, response, error) in
      // Your completion logic
      }
      secondOperation?.addDependency(firstOperation!)
      queue.addOperations([firstOperation!, secondOperation!], waitUntilFinished: false)





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Using TRVSURLSessionOperation would help you, here is how to do it:



        let queue = OperationQueue()
        let session = URLSession(configuration: URLSessionConfiguration.default)
        let firstOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "first url")!)) { (data, response, error) in
        // Your completion logic
        }
        let secondOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "second url")!)) { (data, response, error) in
        // Your completion logic
        }
        secondOperation?.addDependency(firstOperation!)
        queue.addOperations([firstOperation!, secondOperation!], waitUntilFinished: false)





        share|improve this answer














        Using TRVSURLSessionOperation would help you, here is how to do it:



        let queue = OperationQueue()
        let session = URLSession(configuration: URLSessionConfiguration.default)
        let firstOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "first url")!)) { (data, response, error) in
        // Your completion logic
        }
        let secondOperation = TRVSURLSessionOperation(session: session, request: URLRequest(url: URL(string: "second url")!)) { (data, response, error) in
        // Your completion logic
        }
        secondOperation?.addDependency(firstOperation!)
        queue.addOperations([firstOperation!, secondOperation!], waitUntilFinished: false)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        MuhammadBassio

        1,364812




        1,364812






















            Revathi Bhavanam is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Revathi Bhavanam is a new contributor. Be nice, and check out our Code of Conduct.













            Revathi Bhavanam is a new contributor. Be nice, and check out our Code of Conduct.












            Revathi Bhavanam is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372229%2fswift-synchronous-api-calls%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules