2013-11-15 9 views
6

Sto sviluppando un'applicazione in cui voglio chiamare il metodo in coda separata usando dispatch_async. Voglio chiamare quel metodo ripetutamente dopo un certo intervallo di tempo. Ma il metodo non viene chiamato.metodo non chiamato con dispatch_async e ripetendo NSTimer

Non so cosa c'è di sbagliato. Ecco il mio codice:

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 "); 
} 

risposta

9

Se si desidera un timer ripetuto da richiamare su un dispatch_queue_t, utilizzare dispatch_source_create con un DISPATCH_SOURCE_TYPE_TIMER:

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); 

che crea un timer che viene eseguito una volta ogni 20 secondi (3 ° parametro dispatch_source_set_timer), con un margine di manovra di un un secondo (4 ° parametro dispatch_source_set_timer).

Per annullare questo timer, utilizzare dispatch_source_cancel:

dispatch_source_cancel(timer); 
+0

L'aggiunta di quanto sopra a una vista caricata in una vc non genera alcun output della console, cosa manca? –

+1

@DavidKarlsson la variabile 'timer' in realtà deve essere una proprietà di classe (o ivar). A differenza di 'NSTiner', se non rientra nell'ambito, il timer di spedizione viene annullato e deallocato. – Rob

1

Prova questo codice

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 
     ); 
}); 
} 
+1

anche questo è corretto, anche se fonte di spedizione è probabilmente il meccanismo più diretto. Questa risposta potrebbe essere semplificata, tuttavia, poiché non è necessario spedire la creazione del timer a una coda in background e quindi alla coda principale. Basta creare il timer ripetuto nella coda principale senza le due spedizioni annidate. Ancora, +1. – Rob