如何在iphone应用程序中单击按钮时停止循环方法

时间:2013-01-28 07:11:37

标签: iphone objective-c ipad

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

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

-(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{

          }

1 个答案:

答案 0 :(得分:0)

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

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

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

1。使用Grand Central Dispatch:

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。使用线程。