处理 HTTP 连接时 NSTimer 崩溃
和平,我的练习是让应用程序连接,发送和接收数据到远程网络服务器,这是每 10 秒一次,所以想法与 NSTimer 一起工作并将其添加到 runLoop 中,但连接仅建立一次,然后(在接下来的 10 秒)应用程序崩溃。这是我的代码,谢谢您的帮助。
#define HTTP_CONTACT_TIMEOUT 10.0
@implementation HTTPEXERCICEAppDelegate
@synthesize window;
@synthesize HttpConnTimer;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self->HttpConnTimer =
[NSTimer scheduledTimerWithTimeInterval:HTTP_CONTACT_TIMEOUT target:self selector:@selector(Contact:) userInfo:NULL repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self->HttpConnTimer forMode:NSRunLoopCommonModes];
return YES;
}
-(void) Contact:(NSTimer*)ttimer {
NSAutoreleasePool *Pool = [[NSAutoreleasePool alloc] init];
HTTP_Comm *HTTPClient = [[HTTP_Comm alloc] init];
[HTTPClient CommunicateWith:@"http://someURL"];
[HTTPClient release];
[Pool release];
}
Peace , my eXercice is to make the application connect , send and receive data to remote webserver and this is every 10 Secondes so idea goes to work with NSTimer and adding it into a runLoop , but the connection is established only once and then then ( in the next 10 seconds ) the app Crashes . Here is my Code , THANK YOU for HELPING .
#define HTTP_CONTACT_TIMEOUT 10.0
@implementation HTTPEXERCICEAppDelegate
@synthesize window;
@synthesize HttpConnTimer;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self->HttpConnTimer =
[NSTimer scheduledTimerWithTimeInterval:HTTP_CONTACT_TIMEOUT target:self selector:@selector(Contact:) userInfo:NULL repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self->HttpConnTimer forMode:NSRunLoopCommonModes];
return YES;
}
-(void) Contact:(NSTimer*)ttimer {
NSAutoreleasePool *Pool = [[NSAutoreleasePool alloc] init];
HTTP_Comm *HTTPClient = [[HTTP_Comm alloc] init];
[HTTPClient CommunicateWith:@"http://someURL"];
[HTTPClient release];
[Pool release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常可疑的地方是
你确定你在 CommunicateWith: 中保留了 HTTPClient 吗?
之后它要么应该保留,要么不应该使用。
也不要一起使用:
因为scheduledTimerWithTimeInterval...已经将定时器添加到runloop中。仅使用scheduledTimerWithTimeInterval ...或timerWithTimeInterval ...与addTimer ...
very suspicious place is
are you sure you've retained HTTPClient in CommunicateWith:?
It either should be retained or should not be used after that.
Also do not use together:
because scheduledTimerWithTimeInterval... already added timer to runloop. Use only scheduledTimerWithTimeInterval... or timerWithTimeInterval... with addTimer...