如何正确地打开和closures另一个线程上的NSStream
我有一个应用程序连接到另一个线程上使用NSStream服务器。 如果用户决定退出,应用程序也会closures连接。 问题是,我永远不能成功地closuresstream或线程在用户断开连接。 以下是我的代码示例,介绍了如何为networking创build线程并尝试closuresstream:
+ (NSThread*)networkThread { static NSThread *networkThread = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ networkThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkThreadMain:) object:nil]; [networkThread start]; }); return networkThread; } + (void)networkThreadMain:(id)sender { while (YES) { @autoreleasepool { [[NSRunLoop currentRunLoop] run]; } } } - (void)scheduleInThread:(id)sender { [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; [inputStream open]; } - (void)closeThread { [inputStream close]; [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; [inputStream release]; inputStream = nil; }
尝试连接inputstream时调用:
[self performSelector:@selector(scheduleInThread:) onThread:[[self class] networkThread] withObject:nil waitUntilDone:YES];
任何意见是极大的赞赏。
- 如何从JavaScript指定一个字体?
- 当使用iPhone或iOS设备在`iframe`中查看时,为什么`position:fixed`不起作用?
- Xamarin UIPickerView iOS
- 如何获得底部y坐标
- C ++和Swift中的不交集联合
- 使用UIWebView构buildHTML5 iOS应用程序
- iOS Google Drive SDK使用关键“webContentLink”将文件加载到UIWebView中
- 如何在iOS SDK中禁用UIView / UIViewController的“highlight subviews”消息?
- 如何在React Native中使用HealthKit处理后台应用刷新