How can I exit [NSRunLoop runUntilDate]?

I am writing an application that should communicate with a device connected via USB. The application sends and receives data in turn from a device with a fixed time. All Rx / Tx occur in a separate thread, because otherwise the user interface will be blocked. The basic structure looks basically like this. (auto-detection pools and missing things)

-(void)comThread:(id)arg {
  while(state == kIsConnected) {
    // let timers run
    [runLoop runUntilDate:[NSDate distantFuture]];
    // handle data
    if(rxTxState == kRx) {
      // do some stuff to pass data to upper layers
      rxTxState = kTx;
    }
    if(rxTxState == kTx) {
      // do some stuff to send data
      rxTimeoutTimer = [NSTimer scheduledTimer....];
    }
  } 
}

After sending data, the application expects to receive data or rxTimeoutTimerto start, which leads to a retransmission of the packet. The rx operation works because the base layers use asynchronous system calls and invoke the rx handler, which looks mostly like this.

-(void)receiveData:(NSData*)data{
  [rxQueue addObject:data];
  [rxTimeoutTimer invalidate];  // cancel timeout
}

() [runLoop runUntilDate:] receiveData:? Apple , RunLoop. - performSelector:onThread:..., , .

.

+3
4
CFRunLoopStop([runLoop getCFRunLoop]);
+9

runloop (, 0,5 ), :

while(state == kIsConnected) {
  while(!iterationDone) {
    [runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
    //do other stufff
  }
}

-(void)receiveData:(NSData*)data{
  [rxQueue addObject:data];
  [rxTimeoutTimer invalidate];  // cancel timeout
  iterationDone = YES;
}
+4

CFRunLoopStop ([runLoop getCFRunLoop]); CancelPerformSelector

, . [NSRunLoop currentRunLoop]

timerUpdateLocation = [NSTimer scheduledTimerWithTimeInterval:[time intValue] target:self selector:@selector(startTrackingBg) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timerUpdateLocation forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];

, .

[timerUpdateLocation invalidate];

0

, . .

 -(void)comThread:(id)arg {
      BOOL ret = YES;
      rxTimeoutTimer = [NSTimer scheduledTimer....];
      while(ret) {
        // let timers run
        ret = [runLoop runUntilDate:[NSDate distantFuture]];
      } 
    }
    -(void)receiveData:(NSData*)data{
      [rxQueue addObject:data];
      [rxTimeoutTimer invalidate];  // cancel timeout
      iterationDone = YES;
    }

kennytm .

0

Source: https://habr.com/ru/post/1728095/


All Articles