0
[self performSelector:@selector(stopPulling) withObject:nil afterDelay:0.01];

The code is fine. I just think that using NSOperation and block should be the way to go for the future.

I am familiar with NSOperation. I just want to do the same thing with block and NSOperation.

I can do this with GCD already:

int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    <#code to be executed on the main queue after delay#>
});

C'mon. There is something that can be done in GCD that can't be done more easily in NSOperation?

Anonymous White
  • 2,149
  • 3
  • 20
  • 27

3 Answers3

1

I ended up making this:

#import "BGPerformDelayedBlock.h"

@implementation BGPerformDelayedBlock


+ (void)performDelayedBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay
{
    int64_t delta = (int64_t)(1.0e9 * delay);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta), dispatch_get_main_queue(), block);
}

+(void)performSlightlyDelayedBlock:(void (^)(void))block
{
    [self performDelayedBlock:block afterDelay:.1];
}
@end

It's based on an answer in How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?

I think it shouldn't be a category.

Strange that I ended up using GCD.

However, using it is simple. I just do:

    [BGPerformDelayedBlock performSlightlyDelayedBlock:^{
        [UIView animateWithDuration:.3 animations:^{
            [self snapToTheTopOfTheNonHeaderView];
        }];
    }];
Community
  • 1
  • 1
Anonymous White
  • 2,149
  • 3
  • 20
  • 27
1

NSOperationQueue does not provide a mechanism for delayed execution. Use GCD or NSTimer.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
0

Your code is similar to, using a NSTimer setting a selector after 0.01sec with no repeats. This will be called on the main thread.

NSOperation or blocks are used to perform operations in background. These you can use instead of performSelectorInBackground.

If your need is to work in background then go for it. There are many tutorials available to learn 'NSOperationusing 'NSOperationQueue and blocks.

vishy
  • 3,241
  • 1
  • 20
  • 25
  • I already use tons of NSOperationQueue. [NSOperationQueue MainQueue] addoperationwithblock, for example will sort of work too. But I need delay. I want solution with blocks rather than selector. – Anonymous White Oct 23 '12 at 08:03