내 응용 프로그램에서 특정 시간 간격 후에 최신 데이터를 얻으려면 XML을 가져 오려면 서버에 요청을 보내야합니다. 백그라운드에서이 작업을 수행하고 싶습니다. 누구나이 작업을 수행 할 수있는 방법을 제안 할 수 있습니까?백그라운드로 서버에 요청을 반복적으로 보내는 방법은 무엇입니까?
미리 감사드립니다.
내 응용 프로그램에서 특정 시간 간격 후에 최신 데이터를 얻으려면 XML을 가져 오려면 서버에 요청을 보내야합니다. 백그라운드에서이 작업을 수행하고 싶습니다. 누구나이 작업을 수행 할 수있는 방법을 제안 할 수 있습니까?백그라운드로 서버에 요청을 반복적으로 보내는 방법은 무엇입니까?
미리 감사드립니다.
내가 서버에 요청을 보낼 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
사용 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;
backgroundTask 란 무엇입니까? –
인스턴스 변수 __block UIBackgroundTaskIdentifier backgroundTask; – tikhop
그리고이 backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler :^{ [[UIApplication sharedApplication] endBackgroundTask : backgroundTask]; backgroundTask = UIBackgroundTaskInvalid; }]]; dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{ // 시작 URL 요청 }); NSTimer 선택기에서 호출해야합니다. –