简体   繁体   中英

How to return a Boolean in a completion handler in Swift

I'm trying to refactor my code and would like to return a Bool inside a closure . When I try it says it is unused and doesn't work. I can do it another way but I'm repeating code that I don't want to do. How can I go about it.

func tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath) -> Bool {

    // These lines are the one that work but would like to get rid of them
    if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
        if rowConversation.isGroupChat && rowConversation.expired  {
            return true
        }
    }

    self.getRowConversation(pIndexPath: pIndexPath) {
        // how to return true here
    }
    return false
}

private func getRowConversation(pIndexPath: IndexPath, completion pCompletion: () -> Void) {
    if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
        if rowConversation.isGroupChat && rowConversation.expired  {
            ConversationManager.shared.deleteConversationID(rowConversation.conversationID)
            pCompletion()
        }
    }
}

You are probably over-thinking this. No "closure" is needed here; no "completion handler" is needed. Nothing asynchronous is happening. Just turn getRowConversation into an ordinary function that returns a Bool; call it and return the result that it passes back to you.

private func getRowConversation(pIndexPath: IndexPath) -> Bool {
    if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
        if rowConversation.isGroupChat && rowConversation.expired  {
            ConversationManager.shared.deleteConversationID(rowConversation.conversationID)
            return true
        }
    }
    return false
}

And call it like this:

func tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath) -> Bool {
    return self.getRowConversation(pIndexPath: pIndexPath)
}

Your problem is that you want to return a result that is produced asynchronously in getRowConversation(pIndexPath: pIndexPath) before it is delivered, ie immediately after this function is called in tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath) -> Bool .
This is simply not possible, since the result is not yet known at this time.
You had to change (if this is possible) your function tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath) -> Bool so that is also has a callback, eg
tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath, completion: @escaping ((Bool) -> Void)) , and to use the result only in the completion block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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