Comment puis-je m'assurer que ma fonction n'est exécutée que sur le thread principal ? Elle met à jour les éléments de l'interface utilisateur.
Une telle fonction est-elle considérée comme "mauvaise" ?
-(void)updateSomethingOnMainThread {
if ( ![[NSThread currentThread] isEqual:[NSThread mainThread]] )
[self performSelectorOnMainThread:_cmd withObject:nil waitUntilDone:NO];
else {
// Do stuff on main thread
}
}
Je l'ai écrit ainsi pour éviter d'avoir une deuxième fonction, initialement je l'avais comme ça :
-(void)updateSomethingOnMainThread_real {
// Do stuff on main thread
}
-(void)updateSomethingOnMainThread {
[self performSelectorOnMainThread:@selector(updateSomethingOnMainThread_real) withObject:nil waitUntilDone:NO];
}