【问题标题】:Problem with Thread and NSTimer线程和 NSTimer 的问题
【发布时间】:2011-03-21 02:11:29
【问题描述】:

我的代码有问题。 我想启动线程,但是当它启动时,他们无法启动 NSTimer 指令。 你能帮帮我吗?

-(void)detectionMove:(NSTimer*)timer{

    int indexArray = [[[timer userInfo] objectForKey:@"arrayIndex"] intValue];
    // do something
}



-(void)callDectectionMove:(NSNumber*)arrayIndex{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 

    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];  
    [myDictionary setObject:arrayIndex forKey:@"arrayIndex"];  

    //This istruction can't lunch a detectionMove method
    [NSTimer scheduledTimerWithTimeInterval:timeToCatch target:self selector:@selector(detectionMove:) userInfo:myDictionary repeats:NO];   

    [pool   release]; 

}


-(void)detectPositionMovement{


    for(int i = 0; i< [self.arrayMovement count]; i++){

        if((actualAccelerometerX+sensibilityMovement) > [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueX] && (actualAccelerometerX-sensibilityMovement) < [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueX] &&
           (actualAccelerometerY+sensibilityMovement) > [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueY] && (actualAccelerometerY-sensibilityMovement) < [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueY] &&
           (actualAccelerometerZ+sensibilityMovement) > [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueZ] && (actualAccelerometerZ-sensibilityMovement) < [[[[self.arrayMovement      objectAtIndex:i] arrayPositionMove]objectAtIndex:0] valueZ])
            [NSThread detachNewThreadSelector:@selector(callDectectionMove:) toTarget:self withObject:[NSNumber numberWithInt:(int)i]]; 

        }
}

【问题讨论】:

    标签: iphone objective-c nstimer nsthread


    【解决方案1】:

    你真的,真的需要一个不同的线程吗?你想在那个后台线程上启动定时器,为什么不能在主线程上启动定时器呢?

    回答您的问题: 第一个问题是:您的线程没有像 Girish 建议的那样运行足够长的时间。

    第二个问题是:你没有循环线程的runloop。计时器需要运行循环才能工作。

    另见:

    Running NSTimer Within an NSThread?

    Threaded NSTimer

    【讨论】:

      【解决方案2】:

      刚刚解决!关键是在主线程中启动定时器

      [self performSelectorOnMainThread:@selector(startTimer) withObject:nil waitUntilDone:FALSE]; 
      

      希望能帮到你

      【讨论】:

        【解决方案3】:

        您正在威胁中创建 NSTimer 并将计时器对象添加到池中(因为它是类方法),因此在您的计时器被调用之前,计时器对象将被释放(因为线程上下文将在计时器被触发之前结束) .

        有2个解决方案 1. 为 NSTimer 对象添加一个保留计数,并在完成计时器工作后释放。 2. 确保在计时器完成工作之前您的线程不会退出。

        【讨论】:

        • 不太正确。计划的 Timer 被隐式​​保留。没有必要保留它。你的第二个想法可能是正确的。
        • 在类方法返回对象之前,将概念对象添加到当前自动释放池中。在您的情况下,对象“池”是当前的自动释放池,它是在线程退出之前释放的。所以当“池”被释放时,它里面的所有对象都会得到释放消息 - 所以要保留对象,你应该将保留计数增加 1。
        • 我使用示例程序进行了详细研究,其中一些观察结果 1. Timer 在它创建的同一个运行循环上触发,每个线程都有自己的运行循环,所以你的线程应该运行您的计时器会在您提到的时间触发。 2.scheduledTimerWithTimeInterval 返回 NSTimer 对象,保留计数为 '2',因此当自动释放池释放时会减少一个计数,一旦触发计时器事件,就会减少第二个计数。
        猜你喜欢
        • 2011-06-24
        • 1970-01-01
        • 1970-01-01
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        相关资源
        最近更新 更多