Search code examples
memoryswiftuiscrollviewlazyvgrid

matchedGeometryEffect on ScrollView memory leaks


Im using a LazyVGrid to show an article gallery. Everything is working fine, and I have no memory warnings, because every time a view leaves screen, memory usage is reduced.

My problem is when I use matchedGeometryEffect with the image, in order to sync animation with a new View.

Animation works perfect, but the memory is increasing while the scrollview is scrolling. Is like if matchedGeometryEffect was maintaining the memory reference to the object, and don't allow releases.

The container

LazyVGrid(
    columns: [
     GridItem(.flexible())
    ],spacing: 16
 ){
                    
   ForEach(viewModel.articles){ article in
        LazyVStack{
            ArticleCardView(article: article, animation: animation, show: $show)
            .onTapGesture {
                withAnimation(.spring()){
                   selectedArticle = article
                   show.toggle()
                }
            }
        }

    }
 }

The cardView

VStack{
        if !show {
            Image(uiImage: readImage(name: "\(article.id)00"))
                .resizable()
                .aspectRatio(contentMode: .fit)
                //.matchedGeometryEffect(id: "img\(article.id)00", in: animation)

...

The new view

VStack{
    GeometryReader { geo in
       TabView {
           Image(uiImage: readImage(name: "\(article!.id)00"))
               .resizable()
               .aspectRatio(contentMode: .fit)
               //.matchedGeometryEffect(id: "img\(article!.id)00", in: animation, isSource: false)
               .tag(1)

...

Everything works perfect, but if I uncomment commented lines, memory usage increases when scroll.

Any idea? Than you


Solution

  • Solved: I just embed the view into VStack and memories is released:

    ...
    ForEach(viewModel.articles){ article in
         VStack{
            ArticleCardView(article: article, animation: animation, show: $show)...