title: "re-starting" a loop after adding items to an NSMutableArray
body: <p>I have the following code to simulate a process flow in my laboratory. Whereby a timer adds new items to a NSMutablearray. How do I get the while [array count] !=0 loop to recognise the new additions?</p>
  
  <p>Or is there a better methodology?</p>
  
  <p>I'm using GCD blocks to simulate a parallel process. The HEFTBooker object just takes a sample number and then waits a defined time period before "marking" itself as free to process another sample.</p>
  
  <pre><code>-(void)runBookingIn:(id)sender  {
      HEFTLaboratoryUnit *__labUnit = [[HEFTLaboratoryUnit alloc]init];
      self.labUnit = __labUnit;
  
      NSMutableArray *sampleNumbers = [[NSMutableArray alloc]init];
      // Speed factor speeds up time for modelling i.e. 10 = 10 x normal time.
      NSInteger speedFactor = 10;    
      // Sample arrival rate into laboratory per hour
      NSInteger sampleArrivalRate = 50;
  
      __weak id weakSelf = self;
      self.timer = [RNTimer repeatingTimerWithTimeInterval:sampleArrivalRate/speedFactor         block:^{
          [weakSelf addNewSamplesToQueue];
      }];
  
      for (int i = 1; i &lt;= 10; i++) {
          [sampleNumbers addObject:[NSNumber numberWithInteger:i]];
      }
  
      self.labUnit.samplesToBeBookedIn = sampleNumbers;
  
      NSMutableArray *bookerIns = [[NSMutableArray alloc]init];
  
      HEFTBookerIn *webster = [[HEFTBookerIn alloc]init];
      webster.bookingInRate = 60;
      webster.name=@"Webster";
      [bookerIns addObject:webster];
  
      HEFTBookerIn *duffy = [[HEFTBookerIn alloc]init];
      duffy.bookingInRate = 30;
      duffy.name=@"Duffy";
      [bookerIns addObject:duffy];
  
      HEFTBookerIn *marrington = [[HEFTBookerIn alloc]init];
      marrington.bookingInRate = 40;
      marrington.name=@"Marrington";
      [bookerIns addObject:marrington];
  
      HEFTBookerIn *chatha = [[HEFTBookerIn alloc]init];
      chatha.bookingInRate = 10;
      chatha.name=@"Kam";
      [bookerIns addObject:chatha];
  
      int i = 0;
      long countSamples;
  
      countSamples = [self.labUnit.samplesToBeBookedIn count];
      // loop sample numbers
      dispatch_queue_t queue;
      queue = dispatch_queue_create("com.HEFT.BookerQueue", DISPATCH_QUEUE_SERIAL);
  
      dispatch_queue_t arrivalsQueue;
      arrivalsQueue = dispatch_queue_create("com.HEFT.ArrivalsQueue", DISPATCH_QUEUE_SERIAL);
  
      while ([self.labUnit.samplesToBeBookedIn count] != 0 )
      {
          NSInteger sampleNo = [[self.labUnit.samplesToBeBookedIn objectAtIndex:i] integerValue];
          long count = [bookerIns count];
          int counter = 0;
  
          //loop over booker ins
          int j;
          for (j=0; j &lt;count ; j++) {
              HEFTBookerIn *booker = [bookerIns objectAtIndex:counter];
  
              if (booker.free == YES){
                  dispatch_sync(queue, ^{
                      [booker bookingIn:sampleNo :speedFactor];
                      [self.labUnit.samplesToBeBookedIn removeObjectAtIndex:i];
                  });
                  break;
              }
              else{
                  counter ++;
              }
          }
      }
  }
  
  
  -(void) addNewSamplesToQueue{
      NSLog(@"More Samples Arriving");
      for (int i = 1; i &lt;= 10; i++) {
          [self.labUnit.samplesToBeBookedIn addObject:[NSNumber numberWithInteger:i]];
      }
  }
  </code></pre>