* 본 포스트는 Blog.MissFlash.com에서 작성한 것으로, 원문 저작자의 동의없이 마음대로 퍼가실 수 없습니다. 포스트의 내용이 마음에 드시면 링크를 이용해주시면 감사하겠습니다.
> Stanford Lecture #10
* Finding (memory) leaks
* Typical NSThread use case
- (void)someAction:(id)sender{
// Fire up a new thread
[NSThread detachNewThreadSelector:@selector(doWork:) withTarget:self object:someData];
}
- (void)doWork:(id)someData{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[someData doLotsOfWork];
// Message back to the main thread
[self performSelectorOnMainThread:@selector(allDone:) withObject:[someData result] waitUntilDone:NO];
[pool release];
}
* NSLock and subclasses
- (void)someMethod{
[myLock lock];
// We only want one thread executing this code at once
[myLock unlock];
}
* Conditions
// On the producer thread
- (void)produceData{
[condition lock];
newDataExists = YES;
[condition signal];
[condition unlock];
}
// On the consumer thread
- (void)consumeData{
[condition lock];
while(!newDataExists){
[condition wait];
}
newDataExists = NO;
[condition unlock];
}
* Define a custom init method
- (id)initWithSomeObject:(id)someObject{
self = [super init];
if(self){
self.someObject = someObject;
}
return self;
}
* Override - main method to do work
- (void)main{
[someObject doLotsOfTimeConsumingWork];
}
* Using an NSInvocationOperation
- (void)someAction:(id)sender{
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doWork:) object:someObject];
[queue addObject:operation];
[operation release];
}
'PDA&Mobile' 카테고리의 다른 글
아이폰 앱 개발 팁(11) : Head First iPhone Development #2 (4) | 2010.03.23 |
---|---|
아이폰 앱 개발 팁(10) : Head First iPhone Development #1 (0) | 2010.03.18 |
아이폰 앱 개발 팁(8) : Stanford Lecture #9 (0) | 2010.02.12 |
아이폰 앱 개발 팁(7) : Stanford Lecture #8 (0) | 2010.02.08 |
아이폰 앱 개발 팁(6) : Stanford Lecture #7 (0) | 2010.02.01 |