2011-05-09 5 views
0

내 iPhone 앱에서 CMS 기능을 원한다. 인터넷에서 텍스트와 이미지를로드하고 UIImageView, UIButton 및 UITextView에 적용하려고합니다. 그러나 나는 오류가 발생합니다. 현재 코드는 이미지 로딩과 잘 작동합니다. viewDidAppear 코드에 텍스트 코드가 있지만 사용자가 텍스트가로드 될 때까지 화면과 상호 작용할 수 없어서 이미지가있는 loadInfo 메서드로 이동되었지만이 방법이 좋지 않았고 나쁜 액세스가 발생했습니다. 오류. 콘솔에 다음 내용을 인쇄했습니다.백그라운드에서 UIButton 및 UITextView의 텍스트로드 (간단한 CMS)

2011-05-08 21:26:13.770 Fraction Calculator Lite[2184:6b03] bool _WebTryThreadLock(bool), 0x62338d0: 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... 

누구든지 내 문제가 될 수 있습니다.

감사합니다.

- (void)viewDidAppear:(BOOL)animated { 
    [self performSelectorInBackground:@selector(loadInfo) withObject:nil]; 
} 

-(void) loadInfo { 
    NSAutoreleasePool *arPool = [[NSAutoreleasePool alloc] init]; 

    NSError *error; 
    NSString *internetTest = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://lindahlstudios.com/cms/internettest.txt"] encoding:NSUTF8StringEncoding error:&error]; 
    if ([internetTest isEqualToString: @"Internet Connection Complete"]) { 
     UIImage *img1 = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lindahlstudios.com/cms/adimage.png"]]]; 

     [image1 setImage:img1]; 
     [img1 release]; 

     NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://lindahlstudios.com/cms/adtext.txt"] encoding:NSUTF8StringEncoding error:&error]; 
     NSString *string2 = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://lindahlstudios.com/cms/price.txt"] encoding:NSUTF8StringEncoding error:&error]; 

     [adText setText:string]; //Thread 8: Program received signal: "EXC_BAD_ACCESS" 
     [price setTitle:string2 forState:UIControlStateNormal];   
    } 

    [arPool release]; 
} 

답변

0

당신은 백그라운드 스레드에서 UIKit 작업을 호출 할 수 없습니다 :

이 내 코드입니다. 백그라운드 스레드에서 데이터를 얻은 다음 주 스레드에서 업데이트하는 것이 좋습니다. 코딩의 용이성을 위해

사용 블록,

당신이 백그라운드 스레드에있는 NSData를 검색하면, 블록을 통해 메인 스레드에있는 UIImage 등 간단한 할당을,

//get NSData from URL 
    dispatch_async(dispatch_get_main_queue(), ^{ 
      //set assignments for UIImage here. 
    });