-2

Hello everyone, I want to create if loop that running asynchoursly with this code:

NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://localhost/untitled.txt"]];
if (care == @"ONRED") { //untitled.txt = ONRED
   [red_on setHidden:NO];
   [red_off setHidden:YES];
}

How I can run the if statement like a loop?

skaffman
  • 398,947
  • 96
  • 818
  • 769
theShay
  • 247
  • 5
  • 20

1 Answers1

1

If I get you right, you can put those statements in a method and call performSelectorInBackground:

(void)asyncMethod {
    NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://localhost/untitled.txt"]];
    if (care == @"ONRED") { //untitled.txt = ONRED
        [red_on setHidden:NO];
        [red_off setHidden:YES];
    }
}

// in some other method
[self performSelectorInBackground:@selector(asyncMethod) withObject:nil];

Another option is to use the grand central dispatch (as described in this answer):

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
                                         (unsigned long)NULL), ^(void) {
    NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://localhost/untitled.txt"]];
        if (care == @"ONRED") { //untitled.txt = ONRED
            [red_on setHidden:NO];
            [red_off setHidden:YES];
        }
});
Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277