What guaranteed behaviour does onAppear and onDisappear have?

I have created a repo with one demonstration project

https://github.com/PavelKatunin/SectionsAndItems

The whole idea and requirements are described in the readme file.

Long story short: it is an infinity vertical feed, and I want to be able to count reliably how many sections and items in the feed a user has seen.

I have ideas for 3 versions of implementation of this functionality,
but I started with as it seemed at the moment the most straightforward one.

I wanted to track appearance and disappearance of Sections and Items by handling
onAppear and onDisappear callbacks.

And I found out that sometimes onDisappear is not called when a view actually disappears from the screen and also there is no logical order of the calls (as it seems to me).

So I wonder if I’m doing something wrong here?

Here is a log when I scroll through the feed:

SiwftUI event: Section 1 appeared
SiwftUI event: Item 1 appeared
SiwftUI event: Item 2 appeared
SiwftUI event: Item 1 disappeared
SiwftUI event: Item 3 appeared
SiwftUI event: Item 2 disappeared
SiwftUI event: Item 4 appeared
SiwftUI event: Section 2 appeared
SiwftUI event: Item 1 appeared
SiwftUI event: Item 4 disappeared
SiwftUI event: Item 3 disappeared
SiwftUI event: Item 2 appeared
SiwftUI event: Item 1 disappeared
SiwftUI event: Item 3 appeared
SiwftUI event: Section 1 disappeared
SiwftUI event: Section 3 appeared
SiwftUI event: Item 1 appeared
SiwftUI event: Item 3 disappeared
SiwftUI event: Item 2 disappeared
SiwftUI event: Item 2 appeared
SiwftUI event: Item 1 disappeared
SiwftUI event: Item 3 appeared
SiwftUI event: Section 2 disappeared
SiwftUI event: Item 2 disappeared
SiwftUI event: Item 4 appeared

Here is my 3 simple views:

struct ItemView: View {
    
    @State var text: String
    
    var body: some View {
        ZStack {
            Text("(text)").font(.title)
        }
        .onAppear() {
            print("SiwftUI event: Item (text) appeared")
        }
        .onDisappear() {
            print("SiwftUI event: Item (text) disappeared")
        }
    }
    
}

struct SectionView: View {
    
    @State var color: Color
    @State var items: [String]
    var sectionId: String
    
    var body: some View {
        LazyVStack(spacing: 0) {
            ForEach(items, id: .hashValue) { item in
                ItemView(text: item)
                    .frame(height: UIScreen.main.bounds.size.height)
            }
        }
        .onAppear() {
            print("SiwftUI event: Section (sectionId) appeared")
        }
        .onDisappear() {
            print("SiwftUI event: Section (sectionId) disappeared")
        }
        .background(color)
    }
    
}

struct ScrollOffsetFeedView: View {
    
    static func createLocalSections() -> [Section] {
        return [Section(id: "1", color: .green, items: ["1", "2", "3", "4"]),
                Section(id: "2", color: .red, items: ["1", "2", "3"]),
                Section(id: "3", color: .blue, items: ["1", "2", "3", "4", "5"])]
    }
    
    @State var sections: [Section]
    
    init(sections: [Section]) {
        self.sections = sections
    }
    
    var body: some View {
        ZStack {
            ScrollView {
                LazyVStack(spacing: 0) {
                    ForEach(sections, id: .id) { section in
                        SectionView(color: section.color,
                                    items: section.items,
                                    sectionId: section.id)
                    }
                }
                .scrollTargetLayout()
            }
            .scrollTargetBehavior(.paging)
            .ignoresSafeArea()
            .scrollIndicators(.hidden)
            VStack {
                ZStack {
                    makeSectionsCounter()
                        .background(Color(white: 0.0, opacity: 0.5))
                        .cornerRadius(10)
                        .padding()
                }
                Spacer()
            }
        }
    }
    
    @ViewBuilder func makeSectionsCounter() -> some View {
        ZStack {
            Text("Sections completed 0, item 1 / 4")
        }
        .padding()
    }
    
}

How does onAppear and onDisappear work in ScrollView and LazyVStack?

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