我正在制作一个调查应用程序我在开始按钮上将数据上传到服务器,我希望当单击停止按钮时,它应该停止数据上传,这是通过以下方法。
以下是上传按钮开始按钮。
-(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{
}
答案 0 :(得分:0)
评论指出了这个问题。您需要执行以下操作:
要解决,请考虑以下其中一项:
的 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。使用网络库,为您执行此操作。
第3。使用[NSURLConnection + sendAsynchronousRequest:queue:completionHandler:];
的 4。使用操作队列。
的 5。使用线程。