I have a program that download tons of files from the net
I have several threads downloading data.
After each data is called, I need to update the managed object contexts
If 10 threads finish loading at about the same time, then the managed object context will get run 10 times.
The truth is I only need to run it once.
What I want to do is to create a method that accept a block.
What should I do to make a function that receive a block, but if that block has been run less than 1 second ago, it won’t run the block but instead would postpone the second running till 1 seconds no matter how often the function is called.
Mike Ash has already implemented a timer class for this.
You need to init it with a behaviour type, depending on the exact behaviour you want:
MABGTimerDelaymeans every time you callafterDelay:1.0 do:^{ /*code*/ }it will set the fire date back so that it only gets run a full second after the last call.MABGTimerCoalescemeans every time you callafterDelay:1.0 do:^{ /*code*/ }it will set the fire date back so that it only gets run a full second after the first call.If it’s already been run, both behaviours will allow you to run it again, but only after the delay has passed again.