简体   繁体   English

如何解决 Escaping 闭包捕获 'inout' 参数 'data'

[英]How to resolve Escaping closure captures 'inout' parameter 'data'

I have the following function in a separate swift file which I use to make Firebase calls for data:我在一个单独的 swift 文件中有以下 function 文件,我用它来制作 Firebase 数据调用:

func fetchPosts(data: inout [Post]) {
    
    postRef.observe(DataEventType.value) { (snapshot) in
        
        data.removeAll()     // ***Error thrown here***
        
        if snapshot.value is NSNull {
            
            
        } else {
            
            var counter: UInt = 0
            
            for item in snapshot.children {
                
                let userID = (item as! DataSnapshot).key
                
                self.postRef.child(userID).observe(DataEventType.value) { (snap) in
                    
                    counter = counter + 1
                    
                    for child in snap.children {
                        
                        if let snapshot = child as? DataSnapshot,
                            
                            let post = Post(snapshot: snapshot) {

                                data.append(post)   // ***Error thrown here***
                                
                            }

                        if (counter == snapshot.childrenCount) {
                            
                            data = data.sorted(by: { $0.id > $1.id })   // ***Error thrown here***
                            
                        }
                        
                    }
                    
                }
                
            }
            
        }
        
    }
    
}

In my view I have the following:在我看来,我有以下几点:

@State var posts: [Post] = [] // which is the place holder for the posts data

and then i have the following call然后我有以下电话

func fetchPosts() {
    
    postStore.fetchPosts(data: &posts)
    
}

Which calls the function above and passes the [Post] array by reference它调用上面的 function 并通过引用传递 [Post] 数组

My issue is that I get the following error Escaping closure captures 'inout' parameter 'data' in the above function and I can not figure out what I need to do to resolve it??我的问题是我收到以下错误Escaping closure captures 'inout' parameter 'data'并且我无法弄清楚我需要做什么来解决它?

Has anyone ever encountered this error and what do you do to resolve it??有没有人遇到过这个错误,你怎么解决它?

That won't work with @State , instead you have to use completion callback pattern, like这不适用于@State ,而是您必须使用完成回调模式,例如

func fetchPosts() {
    self.posts = [] // reset
    postStore.fetchPosts() { newPosts in
       DispatchQueue.main.async {
         self.posts = newPosts
       }
    }
}

and fetching function并获取 function

func fetchPosts(completion: @escaping ([Post]) -> () ) {

//  ...
        var data: [Post] = []   // data is a local variable
        
        // ... 

        if (counter == snapshot.childrenCount) {
            
            completion(data.sorted(by: { $0.id > $1.id }))
            
        }

//  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM