Unikající uzavření zachycuje non-útěk parametr "funkcí" říká Xcode

0

Otázka

Snažím se provést přechod po dvou API žádosti jsou kompletní a já mám všechny údaje, které potřebuji. Snažím se to udělat, protože žádosti trvat trochu dlouho. Nicméně, když jsem se snažil udělat něco, jako je tento post, mám tyto chyby:

1. Passing a non-escaping function parameter 'anotherFunc' to a call to a non-escaping function parameter can allow re-entrant modification of a variable
2. Escaping closure captures non-escaping parameter 'anotherFunc'
3. Escaping closure captures non-escaping parameter 'function'

Přidal jsem komentář, kde se chyby objeví. Tady je můj kód:

func getUsernameRequest(function: () -> Void) {
            // send an API request to get the userinfo
            let url = "\(K.API.baseAPIEndpoint)/user"
            let headers: HTTPHeaders = [...]
            AF.request(url, headers: headers).responseDecodable(of: UserModel.self) { response in // this is where the #3 error shows up
                if let value = response.value {
                    self.username = value.username
                    // and do other things
                    function()
                } else {
                    offlineBanner()
                }
            }
        }
        
        func getMessagesRequest(function: (()->Void)->Void, anotherFunc:()->Void) {
            let postDetailUrl = "\(K.API.baseAPIEndpoint)/posts/\(postArray[indexPath.row].id)"
            let headers: HTTPHeaders = [...]
            // send an API request to get all the associated messages
            AF.request(postDetailUrl, headers: headers).responseDecodable(of: PostDetailModel.self) { response in // this is the #2 error shows up
               if let value = response.value {
                   self.messages = value.messages
                   function(anotherFunc) // this is where the #1 error shows up
               } else {
                   offlineBanner()
               }
            }
        }
        
        func performSegueToChat() -> Void {
            self.performSegue(withIdentifier: K.Segue.GotoChat, sender: self)
        }
        
        getMessagesRequest(function: getUsernameRequest, anotherFunc: performSegueToChat)

Děkuji předem.

alamofire ios swift
2021-11-11 03:30:06
1

Nejlepší odpověď

1

Udělal jsem to následující kód, a funguje to, jak jsem čekal! (Ale nejsou moc čisté)

        func getUsernameRequest(function: @escaping() -> Void) {
            // send an API request to get the username
            let url = "\(K.API.baseAPIEndpoint)/user"
            AF.request(url, headers: headers).responseDecodable(of: GetUsernameModel.self) { response in
                    if let value = response.value {
                        self.username = value.username
                        function()
                    } else {
                        offlineBanner()
                    }
                }
        }
        
        func getMessagesRequest(function: @escaping(@escaping()->Void)->Void, anotherFunc: @escaping()->Void) {
            let postDetailUrl = "\(K.API.baseAPIEndpoint)/posts/\(postArray[indexPath.row].id)"
            // send an API request to get all the associated messages and put them into messages array
            AF.request(postDetailUrl, headers: headers).responseDecodable(of: PostDetailModel.self) { response in
                if let value = response.value {
                    self.messages = value.messages
                    function(anotherFunc)
                } else {
                    offlineBanner()
                }
            }
        }
        
        func performSegueToChat() -> Void {
            self.performSegue(withIdentifier: K.Segue.GotoChat, sender: self)
        }
        
        getMessagesRequest(function: getUsernameRequest, anotherFunc: performSegueToChat)

2021-11-11 04:36:01

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................