1
A
回答
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;
相关问题
- 1. 发送请求到服务器在后台在ios
- 2. 向服务器发送大量请求
- 3. 发送POST请求后身体不发送到服务器
- 4. 发送请求到服务器iOS
- 5. 发送邮寄请求到服务器
- 6. Android - 发送JSON请求到服务器
- 7. Python:发送请求到protobuf服务器
- 8. 发送http请求到linux服务器
- 9. 在后台向服务器发送数据/重试请求的选项
- 10. 闪光发送请求到另一台服务器
- 11. 如何向roku中的某个服务器发送api请求
- 12. 如何阻止客户端向服务器发送ajax请求?
- 13. 如何向服务器发送登录请求
- 14. 如何从虚拟IP地址向服务器发送请求?
- 15. SAP向HTTPS请求发送REST服务
- 16. 如何在后台向服务器发送内容? ios
- 17. 如何在7天后发送服务器请求?
- 18. C http服务器发送请求到我的服务器?
- 19. 如何使用HTTParty(Net :: HTTP)向服务器发送文件发送POST请求
- 20. 如何更新http请求,并将其发送到另一台Web服务器
- 21. 无法发送HTTP请求到后端服务器
- 22. 通过另一台服务器向服务器发送请求,从最终服务器获得响应
- 23. Geofencing:通过后台服务发送HTTP请求失败。给UnknownHostException
- 24. fetchEntityByKey没有向服务器发送任何请求
- 25. 如何向有服务器的服务器调用请求并在响应无效时再次发送请求?
- 26. 如何发送HttpPost请求到服务器的PHP脚本
- 27. 如何通过angular4发送json请求到api服务器?
- 28. 如何发送删除请求到python服务器
- 29. 如何通过http请求发送文件到http服务器
- 30. 如何请求发送到JSONP服务器煎茶2.0
什么是backgroundTask? – 2012-07-20 11:49:01
实例变量 __block UIBackgroundTaskIdentifier backgroundTask; – tikhop 2012-07-20 11:50:13
并且这个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