简体   繁体   English

GCDAsyncSocket-如何写入可变数据?

[英]GCDAsyncSocket - how do I write mutable data?

The documentation for GCDAsyncSocket says mutable data that might change should be copied before passing it to the write function. GCDAsyncSocket的文档说,可能发生更改的可变数据应先复制,然后再将其传递给写入功能。

In the following code: 在下面的代码中:

func send(buffer: NSMutableData) {
    let bufferCopy = NSData(data: buffer)
    socket.writeData(bufferCopy, withTimeout: -1, tag: 0)
}
  1. Is the call to NSData the right way to copy the buffer? NSData的调用是复制缓冲区的正确方法吗?
  2. Will bufferCopy be retained in memory until writeData (which is async ) finishes, or is it local to send, and destroyed when the function exits? 是否将bufferCopy保留在内存中,直到writeData (为async )完成,或者它是本地发送的,并在函数退出时销毁?

1. Is the call to NSData the right way to copy the buffer? 1.调用NSData是复制缓冲区的正确方法吗?

Yes, it will copy the bytes to a new data object 是的,它将把字节复制到一个新的数据对象

2. Will bufferCopy be retained in memory until writeData (which is async) finishes, or is it local to send, and destroyed when the function exits? 2. bufferCopy是否会保留在内存中,直到writeData(异步)完成,或者它是本地发送的,并在函数退出时销毁?

Yes, it will be retained until writeData finishes. 是的,它将一直保留到writeData完成。 Also it will get deallocated once you return from the function send as there are no any other strong references to it from outside if writeData is synchronous. 另外,一旦您从send函数返回,它将被释放,因为writeData是同步的,则没有其他任何强大的外部引用。 If WriteData is an async call, once the call is completed the data will be released as there won't be any strong ref to it as control would have come out from both send and the WriteData async call. 如果WriteData是一个异步调用,则在调用完成后,数据将被释放,因为不会有任何强烈的引用,因为发送和WriteData异步调用中都将包含控件。 So if this call is asynchronous, that means that call needs to have a strong ref to bufferCopy. 因此,如果此调用是异步的,则意味着该调用需要具有对bufferCopy的强引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM