iOS Development

ios – Swift UI on obtain timer causes scroll view bug

Spread the love


Whats up I’ve a fundamental view in swift ui that makes use of a timer that executes each second and an on obtain technique to do one thing each second. This on obtain causes the view’s scroll view to glitch or have a buggy scroll each time it executes. I need to run an motion each second solely when the precise view is displayed on the display screen. Whats an alternate method to do that that will not mess with the view?

I attempted calling a recursive perform within the on seem which recollects its self each second utilizing a dispatch queue (interacting with a view-scrolling, messes with a traditional timer so I exploit foremost dispatch queue), I really feel like this is not the very best method as it might hold executing even with the view is not proven anymore. I attempted to solely do a recursive name when the scenePhase is lively and the view is displayed (bool flag set on seem/disappear) however generally it nonetheless executes when the view is not proven.

EnvironmentObject var viewModel: MessageViewModel

//timer and on obtain
let timer = Timer.publish(each: 1, on: .foremost, in: .widespread).autoconnect()

.onReceive(timer) { _ in
            if appeared && scenePhase == .lively {
                viewModel.timeRemaining -= 1
                if viewModel.timeRemaining == 0 {
                    viewModel.getMessagesNew()
                    viewModel.timeRemaining = 5.0
                }
            }
        }

//recursive func
func runActionEverySecond() {
        let dispatchTime = DispatchTime.now() + 1.0
  
        DispatchQueue.foremost.asyncAfter(deadline: dispatchTime) {
            print("operating=---------------")
            if appeared && scenePhase == .lively {
                viewModel.timeRemaining -= 1
                if viewModel.timeRemaining == 0 {
                    viewModel.getMessagesNew()
                    viewModel.timeRemaining = 5.0
                }
                runActionEverySecond()
            }
        }
    }

class MessageViewModel: Identifiable, ObservableObject {
    @Printed var timeRemaining = 7.0


    func getMessagesNew(){
        guard let myUID = Auth.auth().currentUser?.uid else { return }
        if let index = currentChat {
            if let final = chats[index].messages?.first(the place: { (chats[index].convo.uid_one == myUID) ? $0.uid_one_did_recieve == true : $0.uid_one_did_recieve == false } )?.timestamp {
                service.getMessagesNew(docID: chats[index].convo.id ?? "", lastdoc: final) { messages in
                    var new = [Message]()
                    if self.chats[index].convo.uid_one == myUID {
                        new = messages.filter({ $0.uid_one_did_recieve == true })
                    } else {
                        new = messages.filter({ $0.uid_one_did_recieve == false })
                    }
                    if !new.isEmpty {
                        for i in 0..<new.depend {
                            if let textual content = new[i].textual content {
                                if let decrypted_text = self.decrypt(textual content: textual content, key: self.chats[index].consumer.publicKey){
                                    new[i].textual content = decrypted_text
                                }
                            }
                        }
                        if let mess = self.chats[index].messages {
                            let newMessages = new.filter { message in
                                !mess.accommodates(the place: { $0.id == message.id })
                            }
                            self.chats[index].messages = newMessages + mess
                            self.chats[index].lastM = newMessages.first
                        } else {
                            self.chats[index].messages = new
                            self.chats[index].lastM = new.first
                        }
                        self.setDate()
                    } else {
                        self.timeRemaining += 8
                    }
                }
            } else if let final = chats[index].messages?.first?.timestamp {
                service.getMessagesNew(docID: chats[index].convo.id ?? "", lastdoc: final) { messages in
                    var new = [Message]()
                    if self.chats[index].convo.uid_one == myUID {
                        new = messages.filter({ $0.uid_one_did_recieve == true })
                    } else {
                        new = messages.filter({ $0.uid_one_did_recieve == false })
                    }
                    if !new.isEmpty {
                        for i in 0..<new.depend {
                            if let textual content = new[i].textual content {
                                if let decrypted_text = self.decrypt(textual content: textual content, key: self.chats[index].consumer.publicKey){
                                    new[i].textual content = decrypted_text
                                }
                            }
                        }
                        if let mess = self.chats[index].messages {
                            let newMessages = new.filter { message in
                                !mess.accommodates(the place: { $0.id == message.id })
                            }
                            self.chats[index].messages = newMessages + mess
                            self.chats[index].lastM = newMessages.first
                        } else {
                            self.chats[index].messages = new
                            self.chats[index].lastM = new.first
                        }
                        self.setDate()
                    } else {
                        self.timeRemaining += 12
                    }
                }
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *