guard let fileId = params.first, fileId.count > 0 else { return } let localFileDataStr = getFileFromLocal(fileId: fileId) if localFileDataStr.count > 0 { h5ProRuntimeCallback(payload, id, data: "'\(localFileDataStr)'") return } getFileFromHiCloud(fileId: fileId) {[weak self] fileDataString, errorCode in guard let weakSelf = self else { return } weakSelf.h5ProRuntimeCallback(payload, id, data: "'\(fileDataString)'", error: errorCode) } 把这段代码放在子线程执行
时间: 2024-01-15 08:03:52 浏览: 119
node.js express框架实现文件上传与下载功能实例详解
你的问题是将这段代码放在子线程执行的方法是什么吗?可以使用GCD中的`DispatchQueue`来实现将这段代码放在子线程执行。具体实现如下:
```
DispatchQueue.global().async {
guard let fileId = params.first, fileId.count > 0 else {
return
}
let localFileDataStr = getFileFromLocal(fileId: fileId)
if localFileDataStr.count > 0 {
h5ProRuntimeCallback(payload, id, data: "'\(localFileDataStr)'")
return
}
getFileFromHiCloud(fileId: fileId) {[weak self] fileDataString, errorCode in
guard let weakSelf = self else {
return
}
weakSelf.h5ProRuntimeCallback(payload, id, data: "'\(fileDataString)'", error: errorCode)
}
}
```
这样,代码块就会在子线程中异步执行。
阅读全文