Đối với "lửa và quên", hãy thử [self performSelectorInBackground:@selector(heavyStuff) withObject:nil]
. Nếu bạn có nhiều thao tác như thế này, bạn có thể muốn xem NSOperation
và lớp con của nó NSInvocationOperation
. NSOperationQueue
quản lý sợi tổng hợp, số lượng đồng thời thực hiện các hoạt động và bao gồm thông báo hoặc ngăn chặn các phương pháp để cho bạn biết khi tất cả các hoạt động hoàn chỉnh:
[self myFoo];
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(heavyStuff) object:nil];
[operationQueue addOperation:operation];
[operation release];
[self myBar];
...
[operationQueue waitUntilAllOperationsAreFinished]; //if you need to block until operations are finished
Ở mức độ thấp hơn, bạn có thể sử dụng sử dụng -[NSThread detachNewThreadSelector:@selector(heavyStuff) toTarget:self withObject:nil]
.
Nguồn
2009-05-28 23:08:33
thiếu 'g' trong performSelectorInBackround – Erich
cố định. cảm ơn @Erich. –