본문 바로가기

PDA&Mobile

아이폰 앱 개발 팁(9) : Stanford Lecture #10

* 본 포스트는 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];
}