2012-07-20 5 views

답변

1

내가 서버에 요청을 보낼 NSOperation을 만들어 위의 문제를 해결하고 response.Its을 구문 분석을 스레드를 사용하는 것보다 훨씬 유용하고 유용합니다.

1.I는 호출 NSTimer 인스턴스 생성 - (공극) sendRequestToGetData은 (NSTimer *) 타이머 특정 시간 간격 후 다음을 :

//Initialize NSTimer to repeat the process after particular time interval... 
    NSTimer *timer = [NSTimer timerWithTimeInterval:60.0 target:self selector:@selector(sendRequestToGetData:) userInfo:nil repeats:YES]; 
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 

I 같이 NSOperation 하위 클래스 NSOperation 생성 sendRequestToGetData 내부 2.Then 다음과 같습니다 :

-(void)sendRequestToGetData:(NSTimer *)timer 
{ 
    //Check whether user is online or not... 
    if(!([[Reachability sharedReachability] internetConnectionStatus] == NotReachable)) 
    { 
     NSURL *theURL = [NSURL URLWithString:myurl]; 
     NSOperationQueue *operationQueue = [NSOperationQueue new]; 
     DataDownloadOperation *operation = [[DataDownloadOperation alloc] initWithURL:theURL]; 
     [operationQueue addOperation:operation]; 
     [operation release]; 
    } 
} 

참고 : DataDownloadOperation은 NSOperation의 하위 클래스입니다.

//DataDownloadOperation.h 

#import <Foundation/Foundation.h> 

@interface DataDownloadOperation : NSOperation 
{ 
    NSURL *targetURL; 
} 
@property(retain) NSURL *targetURL; 
- (id)initWithURL:(NSURL*)url; 

@end 

//DataDownloadOperation.m 
#import "DataDownloadOperation.h" 
#import "XMLParser.h" 

@implementation DataDownloadOperation 
@synthesize targetURL; 

- (id)initWithURL:(NSURL*)url 
{ 
    if (![super init]) return nil; 
    self.targetURL = url; 
    return self; 
} 

- (void)dealloc { 
    [targetURL release], targetURL = nil; 
    [super dealloc]; 
} 

- (void)main { 

    NSData *data = [NSData dataWithContentsOfURL:self.targetURL]; 
    XMLParser *theXMLParser = [[XMLParser alloc]init]; 
    NSError *theError = NULL; 
    [theXMLParser parseXMLFileWithData:data parseError:&theError]; 
    NSLog(@"Parse data1111:%@",theXMLParser.mParsedDict); 
    [theXMLParser release]; 
} 

@end 
2

사용 NSTimer는 대한 반복적으로 요청하고 u는 백그라운드 스레드에서 요청을 수행하려는 경우 u는 그런 일 수행해야합니다

backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{ 
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTask]; 
        backgroundTask = UIBackgroundTaskInvalid; 
       }]; 


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    //start url request 
}); 

//after url request complete 
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask]; 
    backgroundTask = UIBackgroundTaskInvalid; 
+0

backgroundTask 란 무엇입니까? –

+0

인스턴스 변수 __block UIBackgroundTaskIdentifier backgroundTask; – tikhop

+0

그리고이 backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler :^{ [[UIApplication sharedApplication] endBackgroundTask : backgroundTask]; backgroundTask = UIBackgroundTaskInvalid; }]]; dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{ // 시작 URL 요청 }); NSTimer 선택기에서 호출해야합니다. –

관련 문제