Pregunta

I am using a button action to update the value of a MySQL table field. The update is perform in the web server, but I need to update a UILabel text in my view Controller.

This is the code I have implemented:

- (IBAction)votarAction:(id)sender {
    //URL definition where php file is hosted
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

    dispatch_async(backgroundQueue, ^{

        int categoriaID = [[detalleDescription objectForKey:@"idEmpresa"] intValue];

        NSString *string = [NSString stringWithFormat:@"%d", categoriaID];
        NSLog(@"ID EMPRESA %@",string);
        NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/cambiarvaloracionempresa.php?id="];
        [ms appendString:string];
        // URL request
        NSLog(@"URL = %@",ms);
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
        //URL connection to the internet
        NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];



        dispatch_async(dispatch_get_main_queue(), ^{
            //update your label


        });
    });
}
#pragma NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //buffer is the object Of NSMutableData and it is global,so declare it in .h file
    buffer = [NSMutableData data];
    NSLog(@"ESTOY EN didReceiveResponse*********");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
     NSLog(@"ESTOY EN didReceiveDATA*********");
    [buffer appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Here You will get the whole data
     NSLog(@"ESTOY EN didFINISHLOADING*********");
    NSError *jsonParsingError = nil;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:buffer options:0 error:&jsonParsingError];
    //And you can used this array
    NSLog(@"ARRAY = %@",array);

    //HERE LABEL.TEXT UPDATE CODE  

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR de PARSING");
     NSLog(@"ESTOY EN didFAILWITHERROR*********");
}

As I told you, the field value at the MySQL table is updated every time the button is tapped, but the problem is that the NSURLConnection delegate methods are never called. Any help is welcome

¿Fue útil?

Solución

In your view controller's header file add: <NSURLConnectionDelegate>

Also, there's no need to throw the NSURLConnection into a seperate background process, maybe that's why the delegates aren't called. NSURLConnection is already asynchronous

Perhaps try something like this:

- (IBAction)votarAction:(id)sender
{
    int categoriaID = [[detalleDescription objectForKey:@"idEmpresa"] intValue];

    NSString *originalString = [NSString stringWithFormat:@"%d", categoriaID];

    NSMutableString *mutablesString = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/cambiarvaloracionempresa.php?id="];
    [mutableString appendString:originalString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:mutableString]];
    request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
    request.timeoutInterval = 5.0;

    [NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:
     ^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (data)
         {
             NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

             dispatch_async(dispatch_get_main_queue(), ^
             {
                 // Update your label
                 self.label.text = [array objectAtIndex:someIndex];
             });
         }
         else
         {
             // Tell user there's no internet or data failed
         }
     }];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top