1

我使用CFStreamCreateBoundPair创建一对流以上传大文件。代码如下所示。

let readStreamPointer = UnsafeMutablePointer<Unmanaged<CFReadStream>?>.alloc(1)
let writeStreamPointer = UnsafeMutablePointer<Unmanaged<CFWriteStream>?>.alloc(1)
let bufferSize = 1024*1024 //1M buffer size
CFStreamCreateBoundPair(kCFAllocatorDefault, readStreamPointer,writeStreamPointer, Int(bufferSize) as CFIndex)
if let rStream = readStreamPointer.memory?.takeRetainedValue(),writeStream = writeStreamPointer.memory?.takeRetainedValue() {

}

除了内存问题,一切都很顺利。我检查了 Instruments 并发现CFStreamCreateBoundPair无法释放缓冲区大小的内存,在这种情况下,两者都关闭后为 1M CFReadStream。我CFWriteStream也有一个屏幕截图。

仪器屏幕截图 我对 swift 中的 Core Foundation 框架不太熟悉,不知道为什么它不能自动发布。

4

1 回答 1

0

这是我的做法:

class func createBound(inputStream: inout InputStream?, outputStream: inout OutputStream?, bufferSize: UInt) {
    var readStream: Unmanaged<CFReadStream>?
    var writeStream: Unmanaged<CFWriteStream>?
    CFStreamCreateBoundPair(nil, &readStream, &writeStream, CFIndex(bufferSize))
    if inputStream != nil {
        inputStream = readStream!.takeUnretainedValue()
    }
    if outputStream != nil {
        outputStream = writeStream!.takeUnretainedValue()
    }
}
于 2016-12-17T21:35:27.543 回答