从web服务器到iOS获取JSON数组的后台队列


Background queue to get a JSON array from web server to iOS

我正在执行后台队列从web服务器上的MySQL表中获取值。这是我用来获取JSON数组的PHP代码:

$id = $_GET['id'];
$arr = array();
$rs = mysql_query("SELECT * FROM tbcoordenadas where titulo='$id'");
while ($obj = mysql_fetch_assoc($rs)){
$arr[] = $obj['procedencia'];
}
echo json_encode($arr);

这是iOS部分从互联网获取JSON数组的代码:

   - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        NSLog(@"annotation taped TITLE %@",[[view annotation] title  ]      ) ;

        //URL definition where php file is hosted
        dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);
        dispatch_async(backgroundQueue, ^{

            NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/comprobartipo.php?id="];
            // URL request
            NSString *tipo=[[view annotation] title];
            [ms appendString:tipo];
            NSLog(@"TIPO ES AQUI %@", tipo);
            NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
            //URL connection to the internet
            //  NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
            [NSURLConnection sendAsynchronousRequest:request1
                                               queue:[NSOperationQueue mainQueue]
                                   completionHandler:
             ^(NSURLResponse *response, NSData *data1, NSError *connectionError)
             {
                 if (data1)
                 {
                     NSArray *array = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
                     dispatch_async(dispatch_get_main_queue(), ^
                                    {
                                        // Update your label
                                        NSLog(@"PROCEDENCIA DESDE INTERNET %@", [array objectAtIndex:0]);
                                    });
                 }
                 else
                 {
                     // Tell user there's no internet or data failed
                 }
             }];

            dispatch_async(dispatch_get_main_queue(), ^{

            });
        });   
    }

过了一会儿,我检测到编译器游标只到达以下行:

  NSLog(@"PROCEDENCIA DESDE INTERNET %@", [array objectAtIndex:0]);

表示数组中至少有两个对象。通常,查询结果应该只得到一个对象。以后就不可能得到两个对象了,查询只会得到一个对象。

你的代码太复杂了。如果你使用的是sendAsynchronousRequest,就不需要第一个dispatch_async。此外,由于完成处理程序是在主队列上调用的,因此也不需要调用其他dispatch_async。试着不使用它们,看看是否有帮助。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    NSLog(@"annotation taped TITLE %@",[[view annotation] title  ]);
    NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/comprobartipo.php?id="];
    NSString *tipo=[[view annotation] title];
    [ms appendString:tipo];
    NSLog(@"TIPO ES AQUI %@", tipo);
    NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
    [NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data1, NSError *connectionError) {
        if (data1) {
            NSArray *array = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
            NSLog(@"PROCEDENCIA DESDE INTERNET %@", [array objectAtIndex:0]);
        }else{
            // Tell user there's no internet or data failed
        }
    }];
}
编辑后

:

这是我在测试中使用的实际代码。看看它是否适合你。

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/comprobartipo.php?id="];
    NSString *tipo=@"ORIFLAME";
    NSString *tipo2 = @"centro1";
    [ms appendString:tipo];
    //[ms appendString:tipo2];
    NSLog(@"TIPO ES AQUI %@", tipo);
    NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
    [NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data1, NSError *connectionError) {
        if (data1) {
            NSArray *array = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
            NSLog(@"PROCEDENCIA DESDE INTERNET %@", array);
        }else{
            NSLog(@"no data");
        }
    }];
}