Is there any why to call a method after 2second with dispatch, just like performSelector:withObject:afterDelay: ?
            Asked
            
        
        
            Active
            
        
            Viewed 8,426 times
        
    -1
            
            
        - 
                    1StackOverflow has a search function. Use it. Google also works. – Fogmeister Sep 23 '15 at 13:10
- 
                    Yes that link solved my problem.. thanks. – A.T Sep 23 '15 at 13:11
3 Answers
12
            Yes you can,
Swift 3, Swift 4
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
    <# YOUR CODE HERE #>
}
Swift 2
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
        <# YOUR CODE HERE #>
}
Objective C
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    <# YOUR CODE HERE #>
});
 
    
    
        NSTJ
        
- 3,858
- 2
- 27
- 34
 
    
    
        Mehul Sojitra
        
- 1,181
- 9
- 15
- 
                    While this code block may answer the OP's question, this answer would be more useful if you explain how this code works. – Caleb Kleveter Sep 23 '15 at 13:32
- 
                    1For more information about GCD, Refer link: [GCD](https://developer.apple.com/library/prerelease/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/c/func/dispatch_after) – Mehul Sojitra Sep 23 '15 at 13:45
- 
                    
3
            
            
        Yes, we can add a delay to perform GCD with following code
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
 // Do you work over here
});
Hope this will help you.
 
    
    
        sKhan
        
- 9,694
- 16
- 55
- 53
 
    
    
        Nilesh Patel
        
- 6,318
- 1
- 26
- 40
1
            
            
        dispatch_time_t deferTime = 2.0f;
dispatch_after(deferTime, dispatch_get_main_queue(), ^{
//call you method here
});
 
    
    
        Sandeep Ahuja
        
- 931
- 6
- 17
