Waiting for two NSOperation to finish without blocking UI thread









up vote
0
down vote

favorite












I just read long introduction to NSOperationQueues and NSOperation here.



My question is the following. I need to run two operations is the same time. When both those tasks finished I need to make another calculations based on results from two finished operations. If one of the operations fails then whole operation should also fails. Those two operations does not have dependencies and are completely independent from each other so we can run them in parallel.



How to wait for this 2 operation to finish and then continue with calculations? I don't want to block UI Thread. Should I make another NSOperation that main method is creating two NSOperations add them to some local (for this operation) queue and wait with waitUntilAllOperationsAreFinished method. Then continue calculations?



I don't like in this approach that I need to create local queue every time I creating new operation. Can I design it that way that I can reuse one queue but wait only for two local operations? I can imagine that method waitUntilAllOperationsAreFinished can wait until all tasks are done so it will blocks when a lot of tasks will be performed in parallel. Any design advice? Is creating NSOperationQueue expensive? Is there any better ways to do it in iOS without using NSOperation & NSOperationQueue? I'm targeting iOS 9+ devices.










share|improve this question

















  • 1




    Don't wait. Use a DispatchGroup and notify You may not even need NSOperation; it may be sufficient just to dispatch two functions on a concurrent queue
    – Paulw11
    Nov 9 at 20:29







  • 1




    This matches what @Paulw11 wrote about: stackoverflow.com/questions/41412780/…
    – CodeBender
    Nov 9 at 20:42










  • Looks like it is designed for my problem. I just can't find any example in the internet that reuse some calculated objects from individual tasks in notify block. Every example is based on print() something without exchanging data. Thank You guys for point my attention to this. I was not aware of DispatchGroup at all.
    – Marcin Kapusta
    Nov 9 at 20:54














up vote
0
down vote

favorite












I just read long introduction to NSOperationQueues and NSOperation here.



My question is the following. I need to run two operations is the same time. When both those tasks finished I need to make another calculations based on results from two finished operations. If one of the operations fails then whole operation should also fails. Those two operations does not have dependencies and are completely independent from each other so we can run them in parallel.



How to wait for this 2 operation to finish and then continue with calculations? I don't want to block UI Thread. Should I make another NSOperation that main method is creating two NSOperations add them to some local (for this operation) queue and wait with waitUntilAllOperationsAreFinished method. Then continue calculations?



I don't like in this approach that I need to create local queue every time I creating new operation. Can I design it that way that I can reuse one queue but wait only for two local operations? I can imagine that method waitUntilAllOperationsAreFinished can wait until all tasks are done so it will blocks when a lot of tasks will be performed in parallel. Any design advice? Is creating NSOperationQueue expensive? Is there any better ways to do it in iOS without using NSOperation & NSOperationQueue? I'm targeting iOS 9+ devices.










share|improve this question

















  • 1




    Don't wait. Use a DispatchGroup and notify You may not even need NSOperation; it may be sufficient just to dispatch two functions on a concurrent queue
    – Paulw11
    Nov 9 at 20:29







  • 1




    This matches what @Paulw11 wrote about: stackoverflow.com/questions/41412780/…
    – CodeBender
    Nov 9 at 20:42










  • Looks like it is designed for my problem. I just can't find any example in the internet that reuse some calculated objects from individual tasks in notify block. Every example is based on print() something without exchanging data. Thank You guys for point my attention to this. I was not aware of DispatchGroup at all.
    – Marcin Kapusta
    Nov 9 at 20:54












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I just read long introduction to NSOperationQueues and NSOperation here.



My question is the following. I need to run two operations is the same time. When both those tasks finished I need to make another calculations based on results from two finished operations. If one of the operations fails then whole operation should also fails. Those two operations does not have dependencies and are completely independent from each other so we can run them in parallel.



How to wait for this 2 operation to finish and then continue with calculations? I don't want to block UI Thread. Should I make another NSOperation that main method is creating two NSOperations add them to some local (for this operation) queue and wait with waitUntilAllOperationsAreFinished method. Then continue calculations?



I don't like in this approach that I need to create local queue every time I creating new operation. Can I design it that way that I can reuse one queue but wait only for two local operations? I can imagine that method waitUntilAllOperationsAreFinished can wait until all tasks are done so it will blocks when a lot of tasks will be performed in parallel. Any design advice? Is creating NSOperationQueue expensive? Is there any better ways to do it in iOS without using NSOperation & NSOperationQueue? I'm targeting iOS 9+ devices.










share|improve this question













I just read long introduction to NSOperationQueues and NSOperation here.



My question is the following. I need to run two operations is the same time. When both those tasks finished I need to make another calculations based on results from two finished operations. If one of the operations fails then whole operation should also fails. Those two operations does not have dependencies and are completely independent from each other so we can run them in parallel.



How to wait for this 2 operation to finish and then continue with calculations? I don't want to block UI Thread. Should I make another NSOperation that main method is creating two NSOperations add them to some local (for this operation) queue and wait with waitUntilAllOperationsAreFinished method. Then continue calculations?



I don't like in this approach that I need to create local queue every time I creating new operation. Can I design it that way that I can reuse one queue but wait only for two local operations? I can imagine that method waitUntilAllOperationsAreFinished can wait until all tasks are done so it will blocks when a lot of tasks will be performed in parallel. Any design advice? Is creating NSOperationQueue expensive? Is there any better ways to do it in iOS without using NSOperation & NSOperationQueue? I'm targeting iOS 9+ devices.







ios concurrency nsoperationqueue nsoperation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 20:14









Marcin Kapusta

1,89822032




1,89822032







  • 1




    Don't wait. Use a DispatchGroup and notify You may not even need NSOperation; it may be sufficient just to dispatch two functions on a concurrent queue
    – Paulw11
    Nov 9 at 20:29







  • 1




    This matches what @Paulw11 wrote about: stackoverflow.com/questions/41412780/…
    – CodeBender
    Nov 9 at 20:42










  • Looks like it is designed for my problem. I just can't find any example in the internet that reuse some calculated objects from individual tasks in notify block. Every example is based on print() something without exchanging data. Thank You guys for point my attention to this. I was not aware of DispatchGroup at all.
    – Marcin Kapusta
    Nov 9 at 20:54












  • 1




    Don't wait. Use a DispatchGroup and notify You may not even need NSOperation; it may be sufficient just to dispatch two functions on a concurrent queue
    – Paulw11
    Nov 9 at 20:29







  • 1




    This matches what @Paulw11 wrote about: stackoverflow.com/questions/41412780/…
    – CodeBender
    Nov 9 at 20:42










  • Looks like it is designed for my problem. I just can't find any example in the internet that reuse some calculated objects from individual tasks in notify block. Every example is based on print() something without exchanging data. Thank You guys for point my attention to this. I was not aware of DispatchGroup at all.
    – Marcin Kapusta
    Nov 9 at 20:54







1




1




Don't wait. Use a DispatchGroup and notify You may not even need NSOperation; it may be sufficient just to dispatch two functions on a concurrent queue
– Paulw11
Nov 9 at 20:29





Don't wait. Use a DispatchGroup and notify You may not even need NSOperation; it may be sufficient just to dispatch two functions on a concurrent queue
– Paulw11
Nov 9 at 20:29





1




1




This matches what @Paulw11 wrote about: stackoverflow.com/questions/41412780/…
– CodeBender
Nov 9 at 20:42




This matches what @Paulw11 wrote about: stackoverflow.com/questions/41412780/…
– CodeBender
Nov 9 at 20:42












Looks like it is designed for my problem. I just can't find any example in the internet that reuse some calculated objects from individual tasks in notify block. Every example is based on print() something without exchanging data. Thank You guys for point my attention to this. I was not aware of DispatchGroup at all.
– Marcin Kapusta
Nov 9 at 20:54




Looks like it is designed for my problem. I just can't find any example in the internet that reuse some calculated objects from individual tasks in notify block. Every example is based on print() something without exchanging data. Thank You guys for point my attention to this. I was not aware of DispatchGroup at all.
– Marcin Kapusta
Nov 9 at 20:54












1 Answer
1






active

oldest

votes

















up vote
0
down vote













In Swift 4, you can do it this way:



let group = DispatchGroup()
// async
DispatchQueue.global().async
// enter the group
group.enter()
taskA(onCompletion: (_) in
// leave the group
group.leave()
)
group.enter()
taskB(onCompletion: (_) in
group.leave()
)


group.notify(queue: DispatchQueue.main)
// do something on task A & B completion



And there is an excellent tutorial on GCD from raywenderlich.com.






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



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232722%2fwaiting-for-two-nsoperation-to-finish-without-blocking-ui-thread%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













    In Swift 4, you can do it this way:



    let group = DispatchGroup()
    // async
    DispatchQueue.global().async
    // enter the group
    group.enter()
    taskA(onCompletion: (_) in
    // leave the group
    group.leave()
    )
    group.enter()
    taskB(onCompletion: (_) in
    group.leave()
    )


    group.notify(queue: DispatchQueue.main)
    // do something on task A & B completion



    And there is an excellent tutorial on GCD from raywenderlich.com.






    share|improve this answer


























      up vote
      0
      down vote













      In Swift 4, you can do it this way:



      let group = DispatchGroup()
      // async
      DispatchQueue.global().async
      // enter the group
      group.enter()
      taskA(onCompletion: (_) in
      // leave the group
      group.leave()
      )
      group.enter()
      taskB(onCompletion: (_) in
      group.leave()
      )


      group.notify(queue: DispatchQueue.main)
      // do something on task A & B completion



      And there is an excellent tutorial on GCD from raywenderlich.com.






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        In Swift 4, you can do it this way:



        let group = DispatchGroup()
        // async
        DispatchQueue.global().async
        // enter the group
        group.enter()
        taskA(onCompletion: (_) in
        // leave the group
        group.leave()
        )
        group.enter()
        taskB(onCompletion: (_) in
        group.leave()
        )


        group.notify(queue: DispatchQueue.main)
        // do something on task A & B completion



        And there is an excellent tutorial on GCD from raywenderlich.com.






        share|improve this answer














        In Swift 4, you can do it this way:



        let group = DispatchGroup()
        // async
        DispatchQueue.global().async
        // enter the group
        group.enter()
        taskA(onCompletion: (_) in
        // leave the group
        group.leave()
        )
        group.enter()
        taskB(onCompletion: (_) in
        group.leave()
        )


        group.notify(queue: DispatchQueue.main)
        // do something on task A & B completion



        And there is an excellent tutorial on GCD from raywenderlich.com.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 7:00

























        answered Nov 10 at 6:43









        0xa6a

        487518




        487518



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232722%2fwaiting-for-two-nsoperation-to-finish-without-blocking-ui-thread%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo