Create a waiting queue for executing an animation











up vote
1
down vote

favorite












I am currently working on a swift animation. This animation gets triggered by an another function in unsteady time intervals (connected to a server). The animation takes 2 seconds to finish but its possible that it gets triggered before it has finished.
Thats why I was thinking about creating a waiting queue which stores the triggering events until the animation is completed and can be restarted. So on the one hand side I have to lock the animation function until its ready again and one the other I need some kind of storage for the incoming events in the meantime. I was already thinking about dispatch groups but couldnt figure out how I could use them.
I would be really happy about any input in which direction I could go to solve this problem.



Triggering function:



private func subscribeToNewBLock() {
DispatchQueue.global(qos:.userInteractive).async {
watchForNewBlock() {result in
switch result {
case .Failure:
return
case .Success(let result):
//Animation function
self.moveBlocksDown(blockNumber: result)
//Recursive call to keep listening for new blocks
self.subscribeToNewBLock()
}
}
}
}









share|improve this question


























    up vote
    1
    down vote

    favorite












    I am currently working on a swift animation. This animation gets triggered by an another function in unsteady time intervals (connected to a server). The animation takes 2 seconds to finish but its possible that it gets triggered before it has finished.
    Thats why I was thinking about creating a waiting queue which stores the triggering events until the animation is completed and can be restarted. So on the one hand side I have to lock the animation function until its ready again and one the other I need some kind of storage for the incoming events in the meantime. I was already thinking about dispatch groups but couldnt figure out how I could use them.
    I would be really happy about any input in which direction I could go to solve this problem.



    Triggering function:



    private func subscribeToNewBLock() {
    DispatchQueue.global(qos:.userInteractive).async {
    watchForNewBlock() {result in
    switch result {
    case .Failure:
    return
    case .Success(let result):
    //Animation function
    self.moveBlocksDown(blockNumber: result)
    //Recursive call to keep listening for new blocks
    self.subscribeToNewBLock()
    }
    }
    }
    }









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am currently working on a swift animation. This animation gets triggered by an another function in unsteady time intervals (connected to a server). The animation takes 2 seconds to finish but its possible that it gets triggered before it has finished.
      Thats why I was thinking about creating a waiting queue which stores the triggering events until the animation is completed and can be restarted. So on the one hand side I have to lock the animation function until its ready again and one the other I need some kind of storage for the incoming events in the meantime. I was already thinking about dispatch groups but couldnt figure out how I could use them.
      I would be really happy about any input in which direction I could go to solve this problem.



      Triggering function:



      private func subscribeToNewBLock() {
      DispatchQueue.global(qos:.userInteractive).async {
      watchForNewBlock() {result in
      switch result {
      case .Failure:
      return
      case .Success(let result):
      //Animation function
      self.moveBlocksDown(blockNumber: result)
      //Recursive call to keep listening for new blocks
      self.subscribeToNewBLock()
      }
      }
      }
      }









      share|improve this question













      I am currently working on a swift animation. This animation gets triggered by an another function in unsteady time intervals (connected to a server). The animation takes 2 seconds to finish but its possible that it gets triggered before it has finished.
      Thats why I was thinking about creating a waiting queue which stores the triggering events until the animation is completed and can be restarted. So on the one hand side I have to lock the animation function until its ready again and one the other I need some kind of storage for the incoming events in the meantime. I was already thinking about dispatch groups but couldnt figure out how I could use them.
      I would be really happy about any input in which direction I could go to solve this problem.



      Triggering function:



      private func subscribeToNewBLock() {
      DispatchQueue.global(qos:.userInteractive).async {
      watchForNewBlock() {result in
      switch result {
      case .Failure:
      return
      case .Success(let result):
      //Animation function
      self.moveBlocksDown(blockNumber: result)
      //Recursive call to keep listening for new blocks
      self.subscribeToNewBLock()
      }
      }
      }
      }






      swift asynchronous dispatch-queue






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 13:04









      F.Fabian

      124




      124
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You may try to make your animation queue as like below example



          var results = [Int]()
          var isAnimating = false

          private func subscribeToNewBLock() {
          DispatchQueue.global(qos:.userInteractive).async {
          watchForNewBlock() {result in
          switch result {
          case .Failure:
          return
          case .Success(let result):

          //Call your UI operations in main thread
          DispatchQueue.main.async {

          self.results.append(result)
          //Animation function
          self.moveBlocksDown()

          //Recursive call to keep listening for new blocks
          self.subscribeToNewBLock()
          }
          }
          }
          }
          }

          private func moveBlocksDown() {

          guard isAnimating == false && results.count > 0 else {

          return
          }

          self.moveBlocksDown(blockNumber: results.first!)

          }

          private func moveBlocksDown(blockNumber:Int){

          isAnimating = true

          UIView.animate(withDuration: 2.0, animations: {

          //Animation code goes here

          }) { (completed) in

          if completed{

          //Add follwing code in place of animation completed(May be in completion handler)
          self.isAnimating = false
          self.results = self.results.filter{$0 != blockNumber} //Remove already animated blockNumber
          self.moveBlocksDown() //Call moveBlocksDown function to check if anything pending in queue
          }
          }
          }





          share|improve this answer





















          • Thank you very much! It works perfectly. I really didnt expected it to be that simple.
            – F.Fabian
            Nov 9 at 17:46










          • No problem. Have a nice day :)
            – Natarajan
            Nov 10 at 7:56











          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%2f53226266%2fcreate-a-waiting-queue-for-executing-an-animation%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



          accepted










          You may try to make your animation queue as like below example



          var results = [Int]()
          var isAnimating = false

          private func subscribeToNewBLock() {
          DispatchQueue.global(qos:.userInteractive).async {
          watchForNewBlock() {result in
          switch result {
          case .Failure:
          return
          case .Success(let result):

          //Call your UI operations in main thread
          DispatchQueue.main.async {

          self.results.append(result)
          //Animation function
          self.moveBlocksDown()

          //Recursive call to keep listening for new blocks
          self.subscribeToNewBLock()
          }
          }
          }
          }
          }

          private func moveBlocksDown() {

          guard isAnimating == false && results.count > 0 else {

          return
          }

          self.moveBlocksDown(blockNumber: results.first!)

          }

          private func moveBlocksDown(blockNumber:Int){

          isAnimating = true

          UIView.animate(withDuration: 2.0, animations: {

          //Animation code goes here

          }) { (completed) in

          if completed{

          //Add follwing code in place of animation completed(May be in completion handler)
          self.isAnimating = false
          self.results = self.results.filter{$0 != blockNumber} //Remove already animated blockNumber
          self.moveBlocksDown() //Call moveBlocksDown function to check if anything pending in queue
          }
          }
          }





          share|improve this answer





















          • Thank you very much! It works perfectly. I really didnt expected it to be that simple.
            – F.Fabian
            Nov 9 at 17:46










          • No problem. Have a nice day :)
            – Natarajan
            Nov 10 at 7:56















          up vote
          0
          down vote



          accepted










          You may try to make your animation queue as like below example



          var results = [Int]()
          var isAnimating = false

          private func subscribeToNewBLock() {
          DispatchQueue.global(qos:.userInteractive).async {
          watchForNewBlock() {result in
          switch result {
          case .Failure:
          return
          case .Success(let result):

          //Call your UI operations in main thread
          DispatchQueue.main.async {

          self.results.append(result)
          //Animation function
          self.moveBlocksDown()

          //Recursive call to keep listening for new blocks
          self.subscribeToNewBLock()
          }
          }
          }
          }
          }

          private func moveBlocksDown() {

          guard isAnimating == false && results.count > 0 else {

          return
          }

          self.moveBlocksDown(blockNumber: results.first!)

          }

          private func moveBlocksDown(blockNumber:Int){

          isAnimating = true

          UIView.animate(withDuration: 2.0, animations: {

          //Animation code goes here

          }) { (completed) in

          if completed{

          //Add follwing code in place of animation completed(May be in completion handler)
          self.isAnimating = false
          self.results = self.results.filter{$0 != blockNumber} //Remove already animated blockNumber
          self.moveBlocksDown() //Call moveBlocksDown function to check if anything pending in queue
          }
          }
          }





          share|improve this answer





















          • Thank you very much! It works perfectly. I really didnt expected it to be that simple.
            – F.Fabian
            Nov 9 at 17:46










          • No problem. Have a nice day :)
            – Natarajan
            Nov 10 at 7:56













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You may try to make your animation queue as like below example



          var results = [Int]()
          var isAnimating = false

          private func subscribeToNewBLock() {
          DispatchQueue.global(qos:.userInteractive).async {
          watchForNewBlock() {result in
          switch result {
          case .Failure:
          return
          case .Success(let result):

          //Call your UI operations in main thread
          DispatchQueue.main.async {

          self.results.append(result)
          //Animation function
          self.moveBlocksDown()

          //Recursive call to keep listening for new blocks
          self.subscribeToNewBLock()
          }
          }
          }
          }
          }

          private func moveBlocksDown() {

          guard isAnimating == false && results.count > 0 else {

          return
          }

          self.moveBlocksDown(blockNumber: results.first!)

          }

          private func moveBlocksDown(blockNumber:Int){

          isAnimating = true

          UIView.animate(withDuration: 2.0, animations: {

          //Animation code goes here

          }) { (completed) in

          if completed{

          //Add follwing code in place of animation completed(May be in completion handler)
          self.isAnimating = false
          self.results = self.results.filter{$0 != blockNumber} //Remove already animated blockNumber
          self.moveBlocksDown() //Call moveBlocksDown function to check if anything pending in queue
          }
          }
          }





          share|improve this answer












          You may try to make your animation queue as like below example



          var results = [Int]()
          var isAnimating = false

          private func subscribeToNewBLock() {
          DispatchQueue.global(qos:.userInteractive).async {
          watchForNewBlock() {result in
          switch result {
          case .Failure:
          return
          case .Success(let result):

          //Call your UI operations in main thread
          DispatchQueue.main.async {

          self.results.append(result)
          //Animation function
          self.moveBlocksDown()

          //Recursive call to keep listening for new blocks
          self.subscribeToNewBLock()
          }
          }
          }
          }
          }

          private func moveBlocksDown() {

          guard isAnimating == false && results.count > 0 else {

          return
          }

          self.moveBlocksDown(blockNumber: results.first!)

          }

          private func moveBlocksDown(blockNumber:Int){

          isAnimating = true

          UIView.animate(withDuration: 2.0, animations: {

          //Animation code goes here

          }) { (completed) in

          if completed{

          //Add follwing code in place of animation completed(May be in completion handler)
          self.isAnimating = false
          self.results = self.results.filter{$0 != blockNumber} //Remove already animated blockNumber
          self.moveBlocksDown() //Call moveBlocksDown function to check if anything pending in queue
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 13:54









          Natarajan

          2,00931024




          2,00931024












          • Thank you very much! It works perfectly. I really didnt expected it to be that simple.
            – F.Fabian
            Nov 9 at 17:46










          • No problem. Have a nice day :)
            – Natarajan
            Nov 10 at 7:56


















          • Thank you very much! It works perfectly. I really didnt expected it to be that simple.
            – F.Fabian
            Nov 9 at 17:46










          • No problem. Have a nice day :)
            – Natarajan
            Nov 10 at 7:56
















          Thank you very much! It works perfectly. I really didnt expected it to be that simple.
          – F.Fabian
          Nov 9 at 17:46




          Thank you very much! It works perfectly. I really didnt expected it to be that simple.
          – F.Fabian
          Nov 9 at 17:46












          No problem. Have a nice day :)
          – Natarajan
          Nov 10 at 7:56




          No problem. Have a nice day :)
          – Natarajan
          Nov 10 at 7:56


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226266%2fcreate-a-waiting-queue-for-executing-an-animation%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()