0

以下のコードを使用して、NSTimerがトリガーされるのを待ちました

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

    isTriggered=true;

    }


    //----------------------------------------------
    isTriggered=false;

    [NSTimer scheduledTimerWithTimeInterval:1  
                                 target:self
                               selector:@selector(timer1Fired:)
                               userInfo:nil
                                repeats:NO];






    int j1 = 0;
    while ( !isTriggered && j1 <   1000)
    {

        [NSThread sleepForTimeInterval: 0.5];

        j1++;

    }
    //continue to do  something   //b

しかし、NSTimerは決してトリガーされないようです

コメント歓迎

4

1 に答える 1

0

私は[NSThread sleepFortimeInterval]あなたがそれがすることを期待することをしないと思います。そうですisTriggered:、500秒のwhileループが終了するまで実行されません。

また、NSTimerNSThreadはどちらも古風であり、代わりにGrand Central Dispatchを使用する必要があります(両方を置き換えるため)。GCDは大幅に高速で、使用するリソースも少なく、信頼性と能力も高くなっています。

https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

  // do stuff on background thread

  dispatch_sync(dispatch_get_main_queue(), ^{

    // do stuff on main thread

  });
});

低レベルのCAPIの使用を避けたい場合は、GCDの高レベルのラッパーであるNSOperationandを使用できます。NSOperationQueue

于 2012-10-31T04:25:32.060 に答える