Взгляните на NSOperation
. NSOperation
- один из немногих классов какао, который должен быть разделен на подклассы, чтобы быть полезным. Добавив свойство delegate
в свой подкласс NSOperation
, вы можете получать уведомления о завершении операции. Также вы можете добавить свойство userInfo
, чтобы позволить операции передавать произвольные данные делегату
@implementation MyNSOperationSubclass
-(void)main
{
//do operation here
//operationResult is used to report back to the delegate. operationResult could include a userInfo key so that the delegate can have some data passed back, or an error key to indicate success of the operation.
NSDictionary *operationResult;
//Some checks to ensure that the delegate implements operationHasFinished: should be added.
//waitUntilDone: YES locks the main thread
[[self delegate] performSelectorOnMainThread:@selector(operationHasFinished:) withObject:operationResult waitUntilDone: YES];
}
@end