处理 HTTP 连接时 NSTimer 崩溃

发布于 2024-09-13 20:36:45 字数 952 浏览 4 评论 0原文

和平,我的练习是让应用程序连接,发送和接收数据到远程网络服务器,这是每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2024-09-20 20:36:45

非常可疑的地方是

 HTTP_Comm *HTTPClient = [[HTTP_Comm alloc] init];
 [HTTPClient CommunicateWith:@"http://someURL"];
 [HTTPClient release];

你确定你在 CommunicateWith: 中保留了 HTTPClient 吗?
之后它要么应该保留,要么不应该使用。

也不要一起使用:

[NSTimer scheduledTimerWithTimeInterval:HTTP_CONTACT_TIMEOUT target:self selector:@selector(Contact:) userInfo:NULL repeats:YES];

[[NSRunLoop mainRunLoop] addTimer:self->HttpConnTimer forMode:NSRunLoopCommonModes];

因为scheduledTimerWithTimeInterval...已经将定时器添加到runloop中。仅使用scheduledTimerWithTimeInterval ...或timerWithTimeInterval ...与addTimer ...

very suspicious place is

 HTTP_Comm *HTTPClient = [[HTTP_Comm alloc] init];
 [HTTPClient CommunicateWith:@"http://someURL"];
 [HTTPClient release];

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:

[NSTimer scheduledTimerWithTimeInterval:HTTP_CONTACT_TIMEOUT target:self selector:@selector(Contact:) userInfo:NULL repeats:YES];

[[NSRunLoop mainRunLoop] addTimer:self->HttpConnTimer forMode:NSRunLoopCommonModes];

because scheduledTimerWithTimeInterval... already added timer to runloop. Use only scheduledTimerWithTimeInterval... or timerWithTimeInterval... with addTimer...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文