小编dev*_*OP1的帖子

如何将OperationQueue与异步函数一起使用?

我需要运行多个函数,并且函数的执行顺序非常重要。我有两个需要运行的非异步函数,然后需要运行两个异步函数。所有函数必须在前一个函数完成后执行。

到目前为止我有以下内容。但是,异步函数似乎并不遵循我所列出的依赖关系。队列开始后,两个异步函数都会立即执行。

我有什么遗漏的吗?任何帮助表示赞赏!:)

let queue = OperationQueue()


let operation1 = BlockOperation {
    nonAsyncFunc1()
}
let operation2 = BlockOperation {
    nonAsyncFunc2()
}
let operation3 = BlockOperation {
    asyncFunc1()
}
let operation4 = BlockOperation {
    asyncFunc2()
}
                                    
operation2.addDependency(operation1)
operation3.addDependency(operation2)
operation4.addDependency(operation3)
                    
self.queue.addOperation(operation1)
self.queue.addOperation(operation2)
self.queue.addOperation(operation3)
self.queue.addOperation(operation4)
Run Code Online (Sandbox Code Playgroud)

asynchronous swift operationqueue

5
推荐指数
1
解决办法
1865
查看次数

SwiftUI - 如何处理相机权限?

我的 ImagePicker 按预期工作。但是,我正在尝试实现相机权限处理,但我似乎陷入了困境。我收到错误:“函数中缺少返回预期返回 UIImagePickerController”,这是理所当然的。我不确定还能在哪里放置 switch 语句来执行处理。任何帮助表示赞赏。

struct ImagePicker: UIViewControllerRepresentable {
    
    var sourceType: UIImagePickerController.SourceType = .photoLibrary
    
    @Binding var selectedImage: UIImage
    @Binding var alertCameraAccessNeeded: Bool
    @Environment(\.presentationMode) private var presentationMode
    @EnvironmentObject private var userManager: UserManager
    
    let cameraAuthStatus = AVCaptureDevice.authorizationStatus(for: .video)
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        
        switch cameraAuthStatus {
        case .notDetermined: requestCameraPermission()
        case .authorized:
            
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = context.coordinator
            imagePicker.allowsEditing = true
            imagePicker.sourceType = sourceType
            
            return imagePicker
            
        case .restricted, .denied: self.alertCameraAccessNeeded = true
            
        @unknown default:
            fatalError()
        }
        
        
    }
    
    func requestCameraPermission() { …
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

2
推荐指数
1
解决办法
6205
查看次数

标签 统计

swift ×2

asynchronous ×1

ios ×1

operationqueue ×1

swiftui ×1