간단한 스레딩 예제에 문제가 있습니다. 내 방법이 완전하지는 않다는 것을 알지만, 지금부터는 시작 버튼을 누르고 매초마다 카운터를 증가시키는 스레드에서 그 불을 끄고 싶습니다. NSLog가 내 timerThread 메소드에 도착한다고 알려주기 때문에 그것이 IB에 올바르게 연결되어 있다는 것을 알고 있습니다. 그런 다음 updateDisplay 메서드에 도달하지 않고 즉시 초기 myThread로 바로 이동하고 풀을 해제합니다. 따라서 내 프로그램이 실제로 카운터를 증가시키지 않는다고 추측합니다. 나는 잠자 지 않은 간격이나 뭔가에 넣을 것이라고 생각했지만, 결국 나는 이것을 달성하기위한 올바른 방법을 놓치고 있다고 생각합니다. 어떤 생각이라도 좋을 것입니다. 감사!iphone threads 초급 질문, 자동 풀 풀 관리에 문제가 있습니다.
@implementation MainController
-(id)initWithLabel:(UILabel *)label {
if (self = [super init]) {
countLabel = label;
[countLabel retain];
}
return self;
}
-(int)count {
return count;
}
-(void)setCount:(int) value {
count = value;
}
-(void)updateDisplay:(NSTimer *)timer {
NSLog(@"%s", __FUNCTION__);
countLabel.text = [NSString stringWithFormat:@"%i", count];
count++;
}
-(void)timerThread {
NSLog(@"%s", __FUNCTION__);
[NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(updateDisplay:)
userInfo:nil
repeats:YES];
//NSNumber *threadID = [NSNumber numberWithInt:(int)threadID];
// threadLabel = [NSString stringWithFormat:@"%@", threadID];
}
-(void)myThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSNumber *threadID = [NSNumber numberWithInt:(int)threadID];
[self performSelectorOnMainThread:@selector(timerThread)
withObject:nil
waitUntilDone:NO];
// NSLog(@"threadID in myThread: %@", threadID);
[pool release];
}
-(void)startThread {
// threadIndex = 0;
// numThreads = 0;
// NSNumber *threadID = [NSNumber numberWithInt:threadIndex++];
[self performSelectorInBackground:@selector(myThread) withObject:nil];
// NSLog(@"%i", threadIndex);
numThreads++;
}
-(void)myThreadStop {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread exit];
[self performSelectorOnMainThread:@selector(updateDisplay)
withObject:nil
waitUntilDone:NO];
[pool release];
}
-(void)stopThread {
[self performSelectorInBackground:@selector(myThreadStop) withObject:nil];
}
-(void) dealloc {
[countLabel release];
[super dealloc];
}
@end
내가 원하는 것은 스레드가 필요하지 않은 것 같습니다. NSTimer 만 있으면됩니다. – ohho
예, 스레드를 제거하십시오. 그들은 당신이 틀린 경우에 당신에게 각종 문제를 일으키는 원인이 될 수있는 진보 된 기술이다. 타이머의 경우 drawonward의 대답은 우수합니다. –