2016-08-05 90 views
0

我使用iMessage的泡沫創建聊天視圖做工精細的iMessage泡沫調用Web服務freze認爲

https://github.com/kerrygrover/iMessageBubble

當我打電話給我的Web服務在我裏面方法本月底

- (IBAction)sendMessage:(id)sender 

這裏是我的網絡方法調用函數,它給出了成功的結果並將我的聊天發送到服務器,但我的視圖凍結。

NSError *errorReturned = nil; 
    NSString *urlString = @"https://url/methodname」; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
    [dict setObject:employeeId forKey:@"employeeId"]; 
    [dict setObject:employeename forKey:@"employeename"]; 
     NSLog(@"dic=%@",dict); 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions  error:&errorReturned]; 
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody: jsonData]; 

    NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned]; 


    if (errorReturned) 
    { 
     //...handle the error 

    } 
    else 
    { 
     NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@", responseString); 

     //...do something with the returned value 
    } 
} 

回答

1

plz創建一個新隊列不在主隊列上運行此代碼。你正在運行主隊列中的代碼plz使用這個

dispatch_queue_t myQueue = dispatch_queue_create("myQueue", NULL); 

     // execute a task on that queue asynchronously 
     dispatch_async(myQueue, ^{ 
    NSError *errorReturned = nil; 
     NSString *urlString = @"https://url/methodname」; 
     NSURL *url = [NSURL URLWithString:urlString]; 
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
     [request setHTTPMethod: @"POST"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
     NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
     [dict setObject:employeeId forKey:@"employeeId"]; 
     [dict setObject:employeename forKey:@"employeename"]; 
      NSLog(@"dic=%@",dict); 
     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions  error:&errorReturned]; 
     [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"]; 
     [request setHTTPBody: jsonData]; 

     NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned]; 


     if (errorReturned) 
     { 
      //...handle the error 

     } 
     else 
     { 
      NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
      NSLog(@"%@", responseString); 

      //...do something with the returned value 
     } 

     });