I have created a very simple sample project just to make my point using RealmSwift..
// ContentView.swift //
import SwiftUI
import RealmSwift
struct ContentView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
NavigationView {
VStack {
Spacer()
NavigationLink("Listing all meals") {
ListView()
.environmentObject(viewModel)
}
Spacer()
}
}
}
}
// ListView.swift //
import SwiftUI
import RealmSwift
struct ListView: View {
@EnvironmentObject var viewModel: ViewModel
@State var meals = [MealDB]()
var body: some View {
List {
ForEach(meals) { meal in
HStack {
Text("\(meal.title)")
.padding(.leading, 6.0)
Spacer()
Button {
viewModel.model.delete(id: meal.id)
} label: {
Text("Delete")
}
.padding(.trailing, 6.0)
.buttonStyle(.borderless)
}
.onDrag {
return NSItemProvider()
}
}
.onMove(perform: move(from:to:))
}
.onAppear {
updateData()
}
}
func updateData() {
meals.removeAll()
// data from Realm database
for mealItem in viewModel.mealItems {// <<<<<<<<<<
meals.append(mealItem)
}
meals.sort {
(($0).place < (($1).place))
}
}
}
// ViewModel.swift //
import Foundation
import RealmSwift
class ViewModel: ObservableObject {
@Published var model = MealStore()
var mealItems: Results<MealDB> {
model.items
}
}
final class MealStore: ObservableObject {
var config: Realm.Configuration
init() {
config = Realm.Configuration()
}
var realm: Realm {
return try! Realm(configuration: config)
}
var items: Results<MealDB> {
realm.objects(MealDB.self)
}
}
// MealDB.swift //
import Foundation
import RealmSwift
class MealDB: Object, Identifiable {
@objc dynamic var id = ""
@objc dynamic var title = ""
@objc dynamic var order = 0
@objc dynamic var place = 0
override class func primaryKey() -> String? {
"id"
}
}
ListView has a list of meals. Each row comes with a button that lets me delete the corresponding row. And the app will crash inside the updateData function. I have found out that the issue is the way how SwiftUI works and hangs on to the old set of data even after I tap the delete button. So a solution is to 'freeze up' the dataset. And the app won't crash when I tap the delete button.
for mealItem in viewModel.mealItems.freeze() {
...
}
Now, my question is... Are there reasons for not freezing up the dataset? If there is no downside, how come MongoDB just doesn't tell us to do it when we use access a dataset in Realm? Thanks.