0

我正在制作一个调查应用程序,我在开始按钮上将数据上传到服务器,我希望当单击停止按钮时,它应该停止数据上传,这是在以下方法中。

下面是上传按钮开始按钮。

-(IBAction)startSyncButtonAction{
   CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];
   for (int i=0; i<[appDelegate.coffeeArray count]; i++) {

    Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:i];


    int mycount=[appDelegate.coffeeArray count];
    NSLog(@"My Array count is %d",mycount);


        NSString*device_Id=coffeeObj.device_Id;
        NSString*R1=coffeeObj.R1;
        NSString*R2=coffeeObj.R2;
        NSString*R3=coffeeObj.R3;
        NSString*comment=coffeeObj.comment;
            NSString*update_date_time=coffeeObj.update_date_time;



    [appDelegate removeCoffee:coffeeObj];

    int mycount1=[appDelegate.coffeeArray count];


    NSLog(@"My Array After delete is %d",mycount1);


    NSLog(@"device_Id%@",device_Id);
    NSLog(@"R1%@",R1);
    NSLog(@"R2%@",R2);
    NSLog(@"R3%@",R3);
    NSLog(@"comment%@",comment);
    NSLog(@"update_date_time%@",update_date_time);

            NSString *post =[[NSString alloc] initWithFormat:@"device_Id=%@&R1=%@&R2=%@&R3=%@&comment=%@&update_date_time=%@",device_Id,R1,R2,R3,comment,update_date_time];

    NSLog(post);
    NSURL *url=[NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/pfizersurvey/SyncSurveySTD.php"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];



    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);


     }

         }

         -(IBAction)stopButtonAction{

          }
4

1 回答 1

0

评论指出了问题所在。您需要执行以下操作:

  • 避免在主线程上做任何繁重的工作——它会锁定应用程序。
  • 完成繁重的工作后 - 请务必在主线程上更新 UI。否则同步会很慢。

要解决,请考虑以下其中一项:

1. 使用 Grand Central Dispatch:

大中央调度使编写线程应用程序变得容易,而无需直接处理线程。请执行下列操作:

dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(taskQ, ^
{
    NSURL *url=[NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/pfizersurvey/SyncSurveySTD.php"];
   NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

//Now update back on the main thread
dispatch_async(dispatch_get_main_queue(), ^
{
    //Now update the view with your data, to show the outcome of the POST
});
    });

dispatch_release(taskQ);

2.使用网络库,它会为你做这件事。

  • AFNetworking 很受欢迎
  • 我喜欢 LRResty,它只是 NSURLConnection 的一个薄层——基本上完成了我上面写的内容,以及一些其他的事情。

3.使用[NSURLConnection +sendAsynchronousRequest:queue:completionHandler:];

4. 使用操作队列。

5. 使用线程。

于 2013-01-28T08:44:44.290 回答