iphone:在 NSURLRequest 期间未调用 applicationWillTerminate?
我正在开发一个应用程序,用户在某些时候必须等待网络服务的正确响应。这可能需要一些时间,因为它需要网络服务的手动确认。因此,请求的 timeoutInterval 设置得非常高,以防止它过早终止。
当程序返回主屏幕后立即启动应用程序时,就会出现问题。应用程序将无法启动(黑屏),我认为这是因为请求尚未发布并且仍在等待响应(不过我可能是错的)。
我尝试了 applicationWillTerminate 方法,但按下主页按钮时不会调用它。同样,这可能是因为应用程序仍在等待响应,但我们将不胜感激更好的解释:)
另外,有人知道该怎么做吗?
代码:
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlAdress]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10000];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest
returningResponse:&response
error:&error];
I am working on an application where the user at some point must wait for a proper response from a webservice. This might take some time, because it requires a manual confirmation from the webservice. Because of this, the timeoutInterval on the request is set very high to prevent it from terminating too early.
The problem comes when starting the application immediately after the program has returned to the home screen. The application will not start (black screen), and I think it is because the request hasn't been released and is still waiting for a response (I might be wrong though).
I tried the applicationWillTerminate method, but it isn't called when pressing the home button. Again, this might be because the application is still waiting for a response, but a better explanation would be greatly appreciated :)
Also, does someone have any idea on what to do?
code:
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlAdress]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10000];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest
returningResponse:&response
error:&error];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在主线程上发出同步请求将阻塞整个运行循环,直到收到响应或请求超时。在此期间不会调用任何内容,包括 applicationWillTerminate。这样做是一个非常糟糕的主意。您应该为请求分离一个单独的线程,或者创建一个异步 NSURLConnection。
话虽这么说,操作系统在终止您的应用程序时无论如何都会释放所有内存,因此上次启动时的任何内容在重新启动时几乎不可能“留下”。除非您的服务器因应用程序终止时丢失客户端而被阻止。在这种情况下,您必须以可以处理这种情况的方式编写服务器,因为这种情况随时可能发生。
Making a synchronous request on the main thread will block the entire run loop until you receive a response or the request times out. Nothing will get called during this time, including applicationWillTerminate. It is a very bad idea to do this. You should either detach a separate thread for the request or create an asynchronous NSURLConnection.
That being said, the OS will free all memory anyway when it terminates your app so there is little chance that anything from the last launch will be "left over" upon relaunch. Unless your server is blocked because it lost its client when your app terminated. In which case you have to write your server in a way that can handle such a case because it can happen anytime.
如果未调用 applicationWillTerminate 委托方法,
那么我猜将会调用以下委托方法。
If the applicationWillTerminate delegate method is not called,
Then I guess the following delegate method would be getting called.