3

I am using dispatch_after() and dispatch_get_current_queue() to delay a method at the moment. For example, delay for 1 second:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        [self someMethod];
    });

As dispatch_get_current_queue() is deprecated from iOS 6, is there any other equivalent way to do this without creating another separated method for performSelector:withObject:afterDelay:?

A similar question here but still using the deprecated way. Thanks!

EDIT:

From Peter Segerblom's answer:

// Without updating UI
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"Hello World");
});

// updating UI    
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [imageView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:imageView];
});

Cheers!

Community
  • 1
  • 1
EES
  • 1,584
  • 3
  • 20
  • 38

1 Answers1

8

I think you should use dispatch_get_global_queue() instead. It should do about the same i think. If you are only interested in delaying the method and not running in another que you could just use dispatch_get_main_queue(). Just remember that you can't update user interface if you are not in the main que.

Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
Peter Segerblom
  • 2,773
  • 1
  • 19
  • 24
  • 1
    Thanks, both `dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)` and `dispatch_get_main_queue()` work perfectly. – EES Dec 09 '13 at 07:50