Отправка главной очереди - правильный способ сделать это? - PullRequest
0 голосов
/ 01 июня 2018

Я не уверен, как мне использовать DispatchQueue.main.async из фонового потока.Стоит ли использовать его только для кода пользовательского интерфейса или для всего?

Какой из них более энергоэффективен?

Пример:

Только для пользовательского интерфейса

worldMessagesFunctions.delete(wmId: cell.worldMessageId) { response in

    if let response = response {
        if response!.type == 1 {

            // Remove value from the source array of the tableView
            if let index = WorldMessagesStore.shared.worldMessages.index(where: { $0.id! == cell.worldMessageId }) {
                WorldMessagesStore.shared.worldMessages.remove(at: index)

                DispatchQueue.main.async {
                    self.tableView.beginUpdates()
                    self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
                    self.tableView.endUpdates()
                }                    
            }                
            print("Okay")                
        } else {                
            print("Error")                
        }

        DispatchQueue.main.async {
            activityIndicator.stopAnimating()
            activityIndicator.removeFromSuperview()
        }            
    }
}

Или Для всего после получения ответа из фонового потока

worldMessagesFunctions.delete(wmId: cell.worldMessageId) { response in

    DispatchQueue.main.async {

        if let response = response {
            if response!.type == 1 {

                // Remove value from the source array of the tableView
                if let index = WorldMessagesStore.shared.worldMessages.index(where: { $0.id! == cell.worldMessageId }) {
                    WorldMessagesStore.shared.worldMessages.remove(at: index)

                    self.tableView.beginUpdates()
                    self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
                    self.tableView.endUpdates()
                }                
                print("Okay")                
            } else {                
                print("Error")                
            }
            activityIndicator.stopAnimating()
            activityIndicator.removeFromSuperview()
        }        
    }
}

1 Ответ

0 голосов
/ 01 июня 2018

Второй лучше.Во-первых, все может происходить в неправильном порядке, и вы делитесь WorldMessagesStore между потоками.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...