Cocoa has a class ready for execute asynchronous methods, it's NSOperation. NSOperation is a key value coding and key value observing compilant, this means that you can observe some properties to perform different actions in your application, for example, if it finished executing (
isExecuting
).- //We init a new queue
- //Create a new operation
- NSInvocationOperation *operation = [NSInvocationOperation alloc] initWithTarget:self selector:@selector(getAge:) object:@"Nicolas"];
- //Add operation to the queue
- [queue addOperation:operation];
Right here we are creating a new queue and adding an operation to it, this way the callback is in the queue and will be called asynchronously.
So we need to declare the callback
- -(void)getAge:(id)sender
- {
- //Do magic
- }
In this case sender will be an (NSString *) object with value "Nicolas", but you can send whatever object you want as a parameter to the callback.
No hay comentarios:
Publicar un comentario