Fetch new messages while maintaining the ui view

so I am trying to write a chat app in swiftui. I’m using firestore as my database, and I’m trying to fetch more messages while maintaining the ui view. I want a ui update that looks like instagram’s effect (https://youtube.com/shorts/ZBGCDmPy8CE, a loading circle then new messages show in the top of the current ui). But in my own app (https://youtu.be/EnemRxESOm4), when I fetch more messages, the ui will re-render then jump to the message that I want, which makes the user experience laggy. How do I make the effect that is same as instagram?

Also, is there a more elegent way to detect wether the user scrolls to the top of the vstack? I’m using ScrollOffsetPreferenceKey to keep monitor where the view is, which is annoying since I need to keep updating the variable.

These are my code:

struct chatview: View {
    @State private var scrollPosition: CGPoint = .zero
    @State var messages: [Int] = Array(1...30)
    @State var nextElement = 31
    @State var lastScrollPosition = 1
    @State private var lastAddElementTime: Date = Date()
    private let addElementInterval: TimeInterval = 3.0 // only update at most once every three seconds
    
    var body: some View {
        NavigationView {
            ScrollView {
                ScrollViewReader { proxy in
                    VStack {
                        ForEach(messages.reversed(), id: .self) { message in
                            Text("message (message)")
                                .frame(height: 30)
                                .id(message)
                        }
                    }
                    .background(GeometryReader { geometry in
                        Color.clear
                            .preference(key: ScrollOffsetPreferenceKey.self, value: geometry.frame(in: .named("scroll")).origin)
                    })
                    .onPreferenceChange(ScrollOffsetPreferenceKey.self) { value in
                        self.scrollPosition = value
                        if self.scrollPosition.y > 0 {
                            let now = Date()
                            if now.timeIntervalSince(self.lastAddElementTime) >= self.addElementInterval {
                                self.lastAddElementTime = now
                                self.lastScrollPosition = messages.count
                                addElement()
                                DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
                                    withAnimation {
                                        proxy.scrollTo(self.lastScrollPosition, anchor: .top)
                                    }
                                }
                            }
                        }
                    }
                    .onAppear {
                        proxy.scrollTo(1, anchor: .bottom) // show the most recent message at the bottom
                    }
                }
            }
            .coordinateSpace(name: "scroll")
            .navigationTitle("Scroll offset: (scrollPosition.y) (scrollPosition.x)")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
    
    func addElement() { // add 10 messages in each update
        for _ in 0..<10 {
            self.messages.append(nextElement)
            nextElement += 1
        }
    }
}

struct ScrollOffsetPreferenceKey: PreferenceKey {
    static var defaultValue: CGPoint = .zero
    static func reduce(value: inout CGPoint, nextValue: () -> CGPoint) {
    }
}

Of course this is not my actual code, but they have the same concept.

I’m wondering is there a way that I can render the view and scroll in the background, then show the rendered ui on the screen. I asked ai, but none of their solutions solve the problem. Thank you guys!

New contributor

Vince Hsueh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật