Memory leaks in Realm SDK used in iOS swift

296 Views Asked by At

I was working on an iOS project where Realm sdk was used. When I checked the leaks instrument of Xcode, it kept on giving me memory leaks on creation of Realm instance. Then I created a very simple demo app using swiftui and Realm. I looked into memory leaks again and I see the leaks again. I want to know is there a way to get rid of these leaks cuz I checked on internet but there is no such solution.

Memory leak on app start

View to show names list:

 import SwiftUI
 import RealmSwift

 struct NameListView: View {

     @ObservedResults(NameModel.self) var nameList

     var body: some View {
    
         List(nameList) { nameObj in
        Text(nameObj.name)
         }
     }
 }

Name model:

 class NameModel: Object, ObjectKeyIdentifiable {

     @Persisted(primaryKey: true) var id: Int
     @Persisted var name: String
 }

View to add names to Realm

 struct AddNameView: View {

     @State private var name: String = ""
     @ObservedResults(NameModel.self) var nameList

     var body: some View {
    
         HStack {
             TextField("Enter name here...", text: $name)
                 .textFieldStyle(.roundedBorder)

             Button {
                 let obj = NameModel()
                 obj.name = name
                 obj.id = nameList.count
                 $nameList.append(obj)
             } label: {
                 Text("Add")
                     .padding()
             }
         }
         .padding()
     }
 }

And this is my Home view

 struct HomeView: View {
     var body: some View {
    
         VStack{
             NameListView()
                 .padding(.bottom, 5)
             AddNameView()
                 .frame(height: 50)
         }
     }
 }

And the screen looks like this

App screen

0

There are 0 best solutions below