SwiftUI 转义闭包捕获变异的“self"参数 [英] SwiftUI Escaping closure captures mutating 'self' parameter
问题描述
我有一个可以通过两种方式打开的视图.一个带有提供给它的数据,另一个带有对 Firestore 文档的文档引用.我创建了两个构造函数,第一个我提供数据,另一个我提供文档参考.然后我使用此参考进行网络调用位我收到一个错误:
I have a view which can be opened in two ways. One with the data provided to it, and the other with a document reference to a Firestore document. I have created two constructors and in the first one I'm providing the data and in the other one I'm a providing the document reference. Then I'm using this reference to make a network call bit I'm getting an error:
转义闭包捕获变异的self"参数
知道如何解决这个问题吗?
Any idea on how to solve this problem?
@State var request: RequestModel?
init(request: RequestModel) {
self.request = request
}
init(reference: DocumentReference) {
FirestoreService().fetchDocument(documentReference: reference) { (request: RequestModel) in
self.request = request
}
}
推荐答案
对象的初始化程序不能异步执行任何操作.它的工作是立即生成对象,并初始化其所有属性.
An object's initializer cannot do anything asynchronous. Its job is to produce the object immediately, with all its properties initialized.
这篇关于SwiftUI 转义闭包捕获变异的“self"参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!