gpt4 book ai didi

ios - LinkedBlockingQueue iOS Swift 4

转载 作者:行者123 更新时间:2023-11-28 07:34:23 25 4
gpt4 key购买 nike

我正在尝试在 Swift 中实现一些在 Android 上使用 LinkedBlockingQueue 实现的东西。

我正在开发的应用程序使用 Starscream 库使用 WebSocket 连接连接到设备。

应用程序可以向设备发送一条消息,请求数据 block 。如果没有收到回复,则此消息只能发送一次。所有数据 block 将在最后用于创建文件。

从应用程序和设备发送/接收的数据是来自 Proto 文件的序列化数据。

我面临的问题是我不知道如何在需要请求一定数量数据 block 的函数中处理这个问题,因为设备的响应发生在我的函数之外。

func getFile(at index:Int, outputFormat:RecordQuality){

let aFile= fileArray[index]

var bytesRemaining = aFle.lengthBytes
var offset:Int64 = 0

while bytesRemaining > 0{

//This function deals with the webSocket write action and deals with all the proto Classes
readFileReq(fileName: aTrack.name, offset: ofset, length: AppSocketManager.dataReqSize)

isWaitingForData = true
print("\(logClassName) TEST -> Blocking!")
while isWaitingForData{

}

print("\(logClassName) TEST -> End Blocking!")

offset += Int64(2352 * 8)
bytesRemaining -= offset
print("\(logClassName) TEST -> \(offset)")

}

print("\(logClassName) get file finished")

}

aFile 是一个变量类型 ReqFile

private struct ReqFile{
var name:String
var lengthBytes:Int64
var startAddress:UInt32
var isAudio:Bool
var numberBlock:UInt32

init(name:String, lengthBytes:Int64) {
self.name = name
self.lengthBytes = lengthBytes
self.startAddress = 0
self.isAudio = false
self.numberBlock = 0
}

}

另一方面,接收数据的 webSocket 委托(delegate)方法是

func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
/* Deal with data */
isWaitingForData = false
}

此代码永远不会到达 websocketDidReceiveData,因为我基本上阻止了期望 isWaitingData 为假退出的 while,对吗?

有什么想法吗?

最佳答案

经过一些研究,我决定将 Operation 子类化:

import UIKit

class TrackOperation:Operation{

//MARK: Vars
private var _executing = true
private var _finished = false

override var isAsynchronous: Bool{return true}
override var isConcurrent: Bool{return true}

override var isExecuting: Bool{
get{
return _executing
}
set{
willChangeValue(for: \.isExecuting)
_executing = newValue
didChangeValue(for: \.isExecuting)
}
}
override var isFinished: Bool{
get{
return _finished
}
set{
willChangeValue(for: \.isFinished)
_finished = newValue
didChangeValue(for: \.isFinished)
}

}

private (set) var fileName:String
private (set) var bytesRemaining:Int64

var offset:Int64{
return Int64(data.count)
}
private (set) var data:Data = Data()
var incomingData:Data? {
didSet{
updateData()
}
}

/*** Completion Blocks ***/
var onMore:(()->())?



//MARK:- Constructor
init(fileName:String, bytesRemaining:Int64) {

self.fileName = fileName
self.bytesRemaining = bytesRemaining

super.init()

qualityOfService = .background
queuePriority = .normal

}



//MARK:- Lifecycle methods
override func start() {

print("\(logClassName) Start ripping process \(bytesRemaining)")

isExecuting = true
isFinished = false

onMore?()

}


//MARK:- Private methods
private func updateData(){
guard let incomingData = incomingData else { return }

data += incomingData
print("\(logClassName) Total \(offset) of \(bytesRemaining)")

if bytesRemaining > offset{
onMore?()
}
else{
print("\(logClassName) Finished RIPPING")
isExecuting = false
isFinished = true
}

}



//MARK:- Public methods




}

代码在后台运行,并在提供请求后使用 onMore block 请求更多数据。所以它可以这样声明:

func ripTrack(at index:Int, outputFormat:RecordQuality){

let aTrack = cdTrackArray[0]

trackOperation = TrackOperation(fileName: aTrack.name, bytesRemaining: aTrack.lengthBytes)

trackOperation?.onMore = {
print("\(self.logClassName) GIVE More Cookies!")

self.dealWithOperation()

}

trackOperation?.completionBlock = {
print("\(self.logClassName) COMPLETION BLOCK FOR Track Operation")
}

currentState = .ripping
self.trackOperation?.start()

}


func websocketDidReceiveData(socket: WebSocketClient, data: Data) {

trackOperation?.incomingData = data

}

F.Y.I 我在问题中提到的文件是一段数据 block 中的音轨,因为我被撕掉了。

关于ios - LinkedBlockingQueue iOS Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53631165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com