0

アクティビティ ビューを表示していますが、サーバーにデータがアップロードされた後にのみ表示されるのは、メイン スレッドの停止が原因である可能性があります。データのアップロード中にこのアクティビティを表示する方法はありますか。両方同時に。

  -(IBAction)startSyncButtonAction{


UIAlertView* alert= [[UIAlertView alloc] initWithTitle:@"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alert show];

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

indicator.center = CGPointMake(150, 100);
[indicator startAnimating];
[alert addSubview:indicator];

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*R4=coffeeObj.R4;
NSString*R5=coffeeObj.R5;
NSString*R6=coffeeObj.R6;
NSString*R7=coffeeObj.R7;
NSString*R8=coffeeObj.R8;
NSString*R9=coffeeObj.R9;
NSString*R10=coffeeObj.R10;
NSString*R11=coffeeObj.R11;
NSString*R12=coffeeObj.R12;
NSString*R13=coffeeObj.R13;
NSString*R14=coffeeObj.R14;     


NSString*update_date_time=coffeeObj.update_date_time;

    NSString*teritory1=coffeeObj.teritory;



    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(@"R4%@",R4);
    NSLog(@"R4%@",R5);
    NSLog(@"R4%@",R6);
    NSLog(@"R4%@",R7);
    NSLog(@"R4%@",R8);
    NSLog(@"R4%@",R9);
    NSLog(@"R4%@",R10);
    NSLog(@"R4%@",R11);
    NSLog(@"R4%@",R12);
    NSLog(@"R4%@",R13);
    NSLog(@"R4%@",R14);

    NSLog(@"update_date_time%@",update_date_time);

    NSString *post =[[NSString alloc] initWithFormat:@"device_Id=%@&R1=%@&R2=%@&R3=%@&R4=%@&R5=%@&R6=%@&R7=%@&R8=%@&R9=%@&R10=%@&R11=%@&R12=%@&R13=%@&R14=%@&update_date_time=%@&teritory1=%@",device_Id,R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R11,R12,R13,R14,update_date_time,teritory1];



    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);

      }


for (int i=0; i<[appDelegate.coffeeArray count]; i++) {

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


     [appDelegate removeCoffee:coffeeObj];

}




[alert dismissWithClickedButtonIndex:0 animated:YES];

    }
4

2 に答える 2

0

メインスレッドにアラートとアップロードコードを配置できるため、アラート呼び出しを開いたときに、別のスレッドのアップロードメソッド((void)uploadDataなど)を呼び出すことができます。すべてのデータのアップロードが完了するまで、コードはUIをスタックし、UIはアラート用に無料になります。

アクション-(IBAction)startSyncButtonActionで、アラートとUIを呼び出します。

UIAlertView* alert= [[UIAlertView alloc] initWithTitle:@"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alert show];

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

indicator.center = CGPointMake(150, 100);
[indicator startAnimating];
[alert addSubview:indicator];

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*R4=coffeeObj.R4;
NSString*R5=coffeeObj.R5;
NSString*R6=coffeeObj.R6;
NSString*R7=coffeeObj.R7;
NSString*R8=coffeeObj.R8;
NSString*R9=coffeeObj.R9;
NSString*R10=coffeeObj.R10;
NSString*R11=coffeeObj.R11;
NSString*R12=coffeeObj.R12;
NSString*R13=coffeeObj.R13;
NSString*R14=coffeeObj.R14;     


NSString*update_date_time=coffeeObj.update_date_time;

    NSString*teritory1=coffeeObj.teritory;



    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(@"R4%@",R4);
    NSLog(@"R4%@",R5);
    NSLog(@"R4%@",R6);
    NSLog(@"R4%@",R7);
    NSLog(@"R4%@",R8);
    NSLog(@"R4%@",R9);
    NSLog(@"R4%@",R10);
    NSLog(@"R4%@",R11);
    NSLog(@"R4%@",R12);
    NSLog(@"R4%@",R13);
    NSLog(@"R4%@",R14);

    NSLog(@"update_date_time%@",update_date_time);

別のスレッドのメソッドへの呼び出しの最後に

// Perform all the data initializations on a new thread
[NSThread detachNewThreadSelector:@selector(uploadData:) toTarget:self withObject:nil];

次に、別の方法が必要です

   -(void) uploadData:(id) obj 
{
 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);

      }


for (int i=0; i<[appDelegate.coffeeArray count]; i++) {

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


     [appDelegate removeCoffee:coffeeObj];
[self performSelectorOnMainThread:@selector(dataDoneLoading:) withObject:_photos waitUntilDone:NO];
}

そこにすべてのアップロードコードを配置します。最後に「dataDoneLoading」でメインスレッドに再度詰め込みます

于 2013-01-31T07:24:34.607 に答える
0

使用する

[self performSelector:@selector(displayAlertView) withObject:nil afterDelay:0.1];

方法。

于 2013-01-31T08:52:29.513 に答える