检测performSelectorInBackground:withObject的结束:
我的 iOS 应用程序中有一个异步服务器请求:
[self performSelectorInBackground:@selector(doSomething) withObject:nil];
如何检测此操作的结束?
I have an asynchronous server request in my iOS app:
[self performSelectorInBackground:@selector(doSomething) withObject:nil];
How can I detect the end of this operation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 doSomething 方法的末尾放置一个调用?!
Put a call at the end of the doSomething method?!
如果您只是想知道它何时完成(并且不想传回太多数据 - 此时我会推荐一个委托),您可以简单地向通知中心发布通知。
在视图控制器 viewDidLoad 方法中添加:
并在 dealloc 中添加:
If you just want to know when it's finished (and don't want to pass much data back - at which point I would recommend a delegate) you could simply post a notification to the notification center.
within your view controllers viewDidLoad method add:
and within dealloc, add:
您可能希望让您的 doSomething 选择器在完成后返回。 PerformSelectorInBackground 上发生的事情是 API 内部的,如果您想要更多控制,您可能需要使用performSelector:onThread:withObject:waitUntilDone:并定期检查线程上的isFinished通过了。我确信您更关心 doSomething 的完成而不是实际的线程,所以只需从那里发回即可。
You may want to have your doSomething selector post back when it has completed itself. What happens on performSelectorInBackground is internal to the API and if you want more control you may want to use
performSelector:onThread:withObject:waitUntilDone:
and periodically check theisFinished
on the thread passed. I am sure you are more concerned about doSomething finishing than the actual thread though so just post back from there.我创建了一个通用后台处理程序来解决这个问题。只有第一个方法是公共的,因此可以在整个过程中使用。请注意,所有参数都是必需的。
下面是它的调用方式的示例:
I have a general background handler I created that solves this problem. Only the first method is public so it can be used throughout. Note that all parameters are required.
Here's an example of how it's called: