2013-06-18 5 views
0

내 API에서 데이터를 가져 와서 UITextview에 데이터를 쓰려고하는데 오류가 발생했습니다.어떻게 UITextview 내용을 변경할 수 있습니까

이러한 코드는 API에서 datas를 feching 및 uitextview에서 그들을 쓰기됩니다

NSOperationQueue *apiCallsQueue = [[NSOperationQueue alloc] init]; 

NSString *[email protected]"x.com/api.php?id="; 
urlstring =[urlstring stringByAppendingString:self.newsId]; 

NSURL *newsdetail = [NSURL URLWithString:urlstring]; 
newsdetailrequest = [NSURLRequest requestWithURL:newsdetail]; 

@try 
{ 
    [NSURLConnection sendAsynchronousRequest:newsdetailrequest queue:apiCallsQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    { 
     NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 
     [textview setText:[dictionary objectForKey:@"title"]]; 
    }]; 
} 
@catch (NSException *exception) 
{ 
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:@"Error" 
                 delegate:nil 
               cancelButtonTitle:@"Tamam" 
               otherButtonTitles:nil]; 
    [errorView show]; 
} 

오류 출력 :

여기
2013-06-18 16:37:16.214 xpro[6816:5103] bool _WebTryThreadLock(bool), 0x7142ee0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... 
1 0x3571fe9 WebThreadLock 
2 0x15846e -[UITextView setText:] 
3 0x5f00 __39-[NewsDetailViewController viewDidLoad]_block_invoke 
4 0xbdbcf8 __block_global_0 
5 0xb4375a -[NSBlockOperation main] 
6 0xb11453 -[__NSOperationInternal start] 
7 0xb11164 -[NSOperation start] 
8 0xb9da31 __block_global_6 
9 0x49ff53f _dispatch_call_block_and_release 
10 0x4a11014 _dispatch_client_callout 
11 0x4a022e8 _dispatch_root_queue_drain 
12 0x4a02450 _dispatch_worker_thread2 
13 0x96c53e72 _pthread_wqthread 
14 0x96c3bd2a start_wqthread 
(lldb) 

내 Controller.h이다가

@interface NewsDetailViewController : UIViewController 
{ 
NSURLRequest *newsdetailrequest; 
NSMutableData *newsdetailData; 
} 
@property (weak, nonatomic) IBOutlet UINavigationBar *newsdetailTitle; 
@property (weak, nonatomic) IBOutlet NSString *navBarTitle; 
@property (weak, nonatomic) IBOutlet NSString *newsId; 
@property (weak, nonatomic) IBOutlet UITextView *htmlDetail; 

답변

1

시도해보십시오.

UI 업데이트를 위해 기본 스레드로 디스패치합니다.

NSOperationQueue *apiCallsQueue = [[NSOperationQueue alloc] init]; 

NSString *[email protected]"x.com/api.php?id="; 
urlstring =[urlstring stringByAppendingString:self.newsId]; 

NSURL *newsdetail = [NSURL URLWithString:urlstring]; 
newsdetailrequest = [NSURLRequest requestWithURL:newsdetail]; 

@try 
{ 
    [NSURLConnection sendAsynchronousRequest:newsdetailrequest queue:apiCallsQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    { 
     NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 

     dispatch_async(dispatch_get_main_queue(), 
     ^{ 
      [textview setText:[dictionary objectForKey:@"title"]]; 
     }); 
    }]; 
} 
@catch (NSException *exception) 
{ 
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:@"Error" 
                 delegate:nil 
               cancelButtonTitle:@"Tamam" 
               otherButtonTitles:nil]; 
    [errorView show]; 
} 
1

그것은처럼 보인다 백그라운드 스레드에서 UIKit에 액세스하려고합니다. 권장하지 않습니다.

이 시도 : [textview performSelectorOnMainThread:@selector(setText:) withObject:[dictionary objectForKey:@"title"] waitUntilDone:YES];

setText: 메인 스레드에서 호출되도록합니다.

+0

이는 권장하지 않을뿐만 아니라 잘못되어 실패 할 수도 있습니다. –

1

사실 실제로 오류는 발생합니다. UI 코드는 주 스레드에서 실행되어야합니다. 다음은 오류를 수정합니다. 완료 핸 들러 블록에 넣으십시오.

dispatch_async(dispatch_get_main_queue(), ^{ 
            //do ui operations like setting textview text 
           }); 
관련 문제