2

I have the following code...

-(void) SetSerialNumber
{
    NSLog(@"SetSerialNumber");
    NSString *serialNum = textFieldSecond.text;
    if (textFieldSecond.text == nil) {
        [self performSelectorOnMainThread:@selector(display:) withObject:@"Please Enter the serial number" waitUntilDone:YES];
        return;
    }

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void){
        NSString* response;
        [self performSelectorOnMainThread:@selector(display:) withObject:@"Sending Set  Serial Num request" waitUntilDone:YES];
        [[testApp sharedClass] SetSerNumber:serialNum];

        [self performSelectorOnMainThread:@selector(display:) withObject:@"Waiting for Response..." waitUntilDone:YES];
        response = [[Process sharedProcess] readAndProcessData:ACK];
        [self performSelectorOnMainThread:@selector(display:) withObject:rpcresponse waitUntilDone:YES];
    });
}

The method display: puts the message onto a UITextView.

I call this function when a button is pressed. Sometimes the dispatch_async block is not being called. Its just falls through it. However, I can see the log message printed every time.

Can anyone please share what might cause this?

Jonathan Arbogast
  • 9,620
  • 4
  • 35
  • 47
jxgn
  • 741
  • 2
  • 15
  • 36
  • If this function is called when a button is pressed, why the call to the first `performSelectorOnMainThread:withObject:waitUntilDone:`? – Jonathan Arbogast Nov 14 '13 at 19:34
  • ok for that I shouldn't have used performSelectorOnMainThread:,its just for a message for the user, nothing more than that. – jxgn Nov 15 '13 at 04:50

1 Answers1

2

What do you mean by "fall through"? The call is asynchronous, so will not happen there and then. Add a log into the block itself and see whether it gets printed.

Are you sure that when it "falls through" the serialNum isn't nil?

Szabi Tolnai
  • 434
  • 5
  • 5
  • Yes it does falls through when the serialNum isn't nil. And it works sometimes. Plus I get the log whenever I tap on the button but the dispatch_async block doesn't get executed. One more catch is that in the "readAndProcessData", it waits indefinitely for a queue to have data, it might be a problem. Anyways thanks for the heads up. – jxgn Nov 15 '13 at 06:30