GCDAsyncSocket 委托调用不起作用
GCDAsyncSocket Delegate Calls Not Working
我遇到的问题与 This problem someone posted on github 完全相同。除非我添加
,否则 GCDAsyncSocket 不会连接
[NSThread sleepForTimeInterval:0.1]
在线程的最后 post 中,这是 posted
I think I solved this and as I suspected it was completely my fault. The parent object of the socket was being dealloc'd before the connection was made. If I slept on the thread momentarily the connection was made before dealloc which (I'm just shooting in the dark now) took a retain on my parent object (the delegate).
我不明白这意味着什么或如何将其实现到我的代码中。到目前为止,我已尝试将 socket
声明为 (retain)
和 (nonatomic, retain)
以及 (Strong, Nonatomic)
.
感谢您花时间阅读我的问题。
改写你引用的post,这是一个内存管理问题;您需要确保 GCDAsyncSocket 实例保持足够长的时间以建立连接、从服务器获得回复等。如果您使用 属性 将套接字的所有权转移给您自己的 class,那么这意味着拥有该套接字的对象也需要保留。
请记住,套接字对象是异步工作的,因此调用 -connect
并不是等待连接发生的自包含操作。它只是开始在另一个线程上滚动并立即继续前进。
如果没有看到有问题的代码,很难确切地说出您的情况出了什么问题。但是,假设您有类似的东西:
@interface MyController : NSObject
@property (strong, nonatomic) GCDAsyncSocket *socket;
@end
那么最有可能的罪魁祸首是您的 MyController 实例被过早释放,因此您需要检查您的内存管理一直到所有权链,确保您的强引用一直存在,只要您期待他们。
查看 Apple 的内存管理指南可能会有所帮助,以获取有关对象所有权和对象生命周期如何工作的更多详细信息:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html
我遇到的问题与 This problem someone posted on github 完全相同。除非我添加
,否则 GCDAsyncSocket 不会连接[NSThread sleepForTimeInterval:0.1]
在线程的最后 post 中,这是 posted
I think I solved this and as I suspected it was completely my fault. The parent object of the socket was being dealloc'd before the connection was made. If I slept on the thread momentarily the connection was made before dealloc which (I'm just shooting in the dark now) took a retain on my parent object (the delegate).
我不明白这意味着什么或如何将其实现到我的代码中。到目前为止,我已尝试将 socket
声明为 (retain)
和 (nonatomic, retain)
以及 (Strong, Nonatomic)
.
感谢您花时间阅读我的问题。
改写你引用的post,这是一个内存管理问题;您需要确保 GCDAsyncSocket 实例保持足够长的时间以建立连接、从服务器获得回复等。如果您使用 属性 将套接字的所有权转移给您自己的 class,那么这意味着拥有该套接字的对象也需要保留。
请记住,套接字对象是异步工作的,因此调用 -connect
并不是等待连接发生的自包含操作。它只是开始在另一个线程上滚动并立即继续前进。
如果没有看到有问题的代码,很难确切地说出您的情况出了什么问题。但是,假设您有类似的东西:
@interface MyController : NSObject
@property (strong, nonatomic) GCDAsyncSocket *socket;
@end
那么最有可能的罪魁祸首是您的 MyController 实例被过早释放,因此您需要检查您的内存管理一直到所有权链,确保您的强引用一直存在,只要您期待他们。
查看 Apple 的内存管理指南可能会有所帮助,以获取有关对象所有权和对象生命周期如何工作的更多详细信息:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html