2012-07-20 129 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]; 

2.然后内部sendRequestToGetData我创建的NSOperation通过继承的NSOperation作为如下:

-(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反复请求,如果ü要执行在后台线程请求ü应该做这样的事情:

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? – 2012-07-20 11:49:01

+0

实例变量 __block UIBackgroundTaskIdentifier backgroundTask; – tikhop 2012-07-20 11:50:13

+0

并且这个backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ {[UIApplication sharedApplication] endBackgroundTask:backgroundTask]; backgroundTask = UIBackgroundTaskInvalid; }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ { // start url request });需要在NSTimer选择器中调用。 – 2012-07-20 11:52:10

相关问题