2013-11-15 9 views
6

दोहरा रहा है मैं एक ऐप विकसित कर रहा हूं जहां मैं dispatch_async का उपयोग करके अलग कतार में विधि को कॉल करना चाहता हूं। मैं समय के कुछ अंतराल के बाद बार-बार उस विधि को कॉल करना चाहता हूं। लेकिन विधि को बुलाया नहीं जा रहा है।विधि dispatch_async के साथ नहीं बुलाया गया है और एनएसटीमर

मुझे नहीं पता कि क्या गलत है। यहाँ मेरी कोड है: के लिए और अधिक

How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?

उत्तर

9

यदि आप चाहते हैं एक दोहराई जाने वाली घड़ी एक dispatch_queue_t पर लागू किया जा करने की कोशिश करो, dispatch_source_create का उपयोगके साथ करें:

dispatch_queue_t queue = dispatch_queue_create("com.firm.app.timer", 0); 
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); 
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 20ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC); 

dispatch_source_set_event_handler(timer, ^{ 

    // stuff performed on background queue goes here 

    NSLog(@"done on custom background queue"); 

    // if you need to also do any UI updates, synchronize model updates, 
    // or the like, dispatch that back to the main queue: 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     NSLog(@"done on main queue"); 
    }); 
}); 

dispatch_resume(timer); 

कि एक टाइमर है कि एक बार हर 20 सेकंड रन (dispatch_source_set_timer को 3 पैरामीटर) बनाता है, (dispatch_source_set_timer को 4 पैरामीटर) एक एक दूसरे के एक छूट के साथ।

इस टाइमर को रद्द करने के dispatch_source_cancel का उपयोग करें:

dispatch_source_cancel(timer); 
+0

बस उपरोक्त को देखने के लिए एक वीसी में लोड किया गया कोई कंसोल आउटपुट उत्पन्न नहीं करता है, क्या गुम है? –

+1

@ डेविडकर्ल्सन 'टाइमर' वैरिएबल वास्तव में एक वर्ग संपत्ति (या ivar) होना चाहिए। 'NSTiner' के विपरीत, यदि यह दायरे से बाहर हो जाता है, तो प्रेषण टाइमर रद्द कर दिया जाता है और हटा दिया जाता है। – Rob

0
int parameter1 = 12; 
float parameter2 = 144.1; 

// Delay execution of my block for 10 seconds. 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{ 
    NSLog(@"parameter1: %d parameter2: %f", parameter1, parameter2); 
}); 

नज़र इस कोड

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    dispatch_async(dispatch_get_main_queue(), ^{ 

     timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self 
               selector: @selector(gettingNotification) userInfo: nil repeats: YES]; 
     // Add code here to update the UI/send notifications based on the 
     // results of the background processing 

    }); 
}); 

-(void)gettingNotification { 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     //background task here 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     // update UI here 
     ); 
}); 
} 
+0

लेकिन फिर कैसे मैं बार-बार – user2185354

1

dispatch_async(NotificationQueue, ^{ 

     NSLog(@"inside queue"); 
     timer = [NSTimer scheduledTimerWithTimeInterval: 20.0 
               target: self 
               selector: @selector(gettingNotification) 
               userInfo: nil 
               repeats: YES]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Add code here to update the UI/send notifications based on the 
      // results of the background processing 

     }); 
    }); 

-(void)gettingNotification { 
    NSLog(@"calling method "); 
} 
+1

विधि कॉल करेंगे हालांकि प्रेषण स्रोत शायद और अधिक प्रत्यक्ष तंत्र है यह भी सही है, तो। हालांकि, यह उत्तर सरलीकृत किया जा सकता है, क्योंकि टाइमर के निर्माण को पृष्ठभूमि कतार में और फिर मुख्य कतार में प्रेषण करने में कोई बात नहीं है। केवल दो घोंसले प्रेषणों के बिना मुख्य कतार में दोहराव टाइमर बनाएं। फिर भी, +1। – Rob

संबंधित मुद्दे