mvasco
mvasco

Reputation: 5101

Exception when NSTimer launched

I have a NSTimer in the viewDidLoad method:

timer = [NSTimer scheduledTimerWithTimeInterval: 10.0
                                         target: self
                                       selector: @selector(refrescar_vehiculo_asignado:)
                                       userInfo: nil
                                        repeats: YES];

The launched selector is this one:

-(void) refrescar_vehiculo_asignado {

    NSURL *apiURL = [NSURL URLWithString:
                     [NSString stringWithFormat:@"http://..hidden here../?employee=%@", _employee]];
    NSURLRequest *request = [NSURLRequest requestWithURL:apiURL]; // this is using GET, for POST examples see the other answers here on this page
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               if(data.length) {
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   if(responseString && responseString.length) {
                                       NSLog(@"dATOS RECIBIDOS=%@", responseString);
                                       NSError *jsonError;
                                       NSData *objectData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
                                       NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                                                            options:NSJSONReadingMutableContainers
                                                                                              error:&jsonError];
                                       NSArray *messageArray = [json objectForKey:@"objects"];

                                       // Parse and loop through the JSON
                                       for (dataDict in messageArray) {
                                           NSString * latstring = [dataDict objectForKey:@"current_latitude"];
                                           NSString * lonstring = [dataDict objectForKey:@"current_longitude"];

                                           NSDictionary *level2Dict = [dataDict objectForKey:@"employee"];
                                           id someObject = [level2Dict objectForKey:@"name"];
                                           NSLog(@"NOMBRE===%@",someObject);
                                           NSString * nombre = someObject;
                                           latdouble = [latstring doubleValue];
                                           londouble = [lonstring doubleValue];

                                           CLLocationCoordinate2D vehiculo = [mapView centerCoordinate];
                                           vehiculo.latitude = latdouble;
                                           vehiculo.longitude = londouble;


                                           PinAsignado *vehiculoDisponible = [[PinAsignado alloc] initWithTitle:@"Vehiculo disponible" location:vehiculo];
                                           vehiculoDisponible.title = nombre;

                                           [self.mapView addAnnotation:vehiculoDisponible];
                                           [mapView selectAnnotation:vehiculoDisponible animated:NO];

                                       }



                                   }
                               }
                           }];
}

This method is launched first in the viewDidLoad method, without any issue, but when it is launched at the scheduled time, it throws an exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MapaConductor refrescar_vehiculo_asignado:]: unrecognized selector sent to instance 

I am not able to find the issue source.

Upvotes: 0

Views: 45

Answers (1)

dehlen
dehlen

Reputation: 7389

It is because you specify the selector like so: refrescar_vehiculo_asignado: but you only implement a method like : refrescar_vehiculo_asignado.

See the difference by removing the colon which indicates whether a method takes parameters or not.

Upvotes: 6

Related Questions