Index out of bounds when using Realm with SwiftUI

2020-05-24 06:02发布

I've been playing around with SwiftUI a bit and have been writing a small meal planner/todo list style app. I was able to get Realm working with SwiftUI and wrote a small wrapper object to get Realm change notifications to update the UI. This works great for adding items and the UI gets properly updated. However, when deleting an item using swipe to delete or other methods, I get an index out of bounds error from Realm.

Here's some code:

ContentView:

    struct ContentView : View {

    @EnvironmentObject var userData: MealObject
    @State var draftName: String = ""
    @State var isEditing: Bool = false
    @State var isTyping: Bool = false

    var body: some View {
        List {
            HStack {
                TextField($draftName, placeholder: Text("Add meal..."), onEditingChanged: { editing in
                    self.isTyping = editing
                },
                onCommit: {
                    self.createMeal()
                    })
                if isTyping {
                    Button(action: { self.createMeal() }) {
                        Text("Add")
                    }
                }
            }
            ForEach(self.userData.meals) { meal in
                NavigationLink(destination: DetailMealView(ingredientsObject: IngredientsObject(meal: meal))) {
                    MealRow(name: meal.name)
                }
            }.onDelete(perform: delete)
        }
        .navigationBarTitle(Text("Meals"))
    }

    func delete(at offsets: IndexSet) {
        guard let index = offsets.first else {
            return
        }
        let mealToDelete = userData.meals[index]
        Meal.delete(meal: mealToDelete)
        print("Meals after delete: \(self.userData.meals)")
    }
}

And the MealObject wrapper class:

final class MealObject: BindableObject {
    let willChange = PassthroughSubject<MealObject, Never>()

    private var token: NotificationToken!
    var meals: Results<Meal>

    init() {
        self.meals = Meal.all()
        lateInit()
    }

    func lateInit() {
        token = meals.observe { changes in
            self.willChange.send(self)
        }
    }

    deinit {
        token.invalidate()
    }
}

I was able to narrow the issue down to

   ForEach(self.userData.meals) { meal in
      NavigationLink(destination: DetailMealView(ingredientsObject: IngredientsObject(meal: meal))) {
      MealRow(name: meal.name)
     }
   }

It seems like self.userData.meals isn't updating, even though when checking the change notification in MealObject it shows the correct deletions and the meals variable in MealObject correctly updates as well.

*Edit: Also to add, the deletion does actually happen and when launching the app again, the deleted item is gone. It seems like SwiftUI gets confused about the state and tries to access the deleted item after willChange gets called.

*Edit 2: Found one workaround for now, I implemented a method checking whether the object currently exists in Realm:

    static func objectExists(id: String, in realm: Realm = try! Realm()) -> Bool {
        return realm.object(ofType: Meal.self, forPrimaryKey: id) != nil
    }

Called like this

            ForEach(self.userData.meals) { meal in
                if Meal.objectExists(id: meal.id) {
                    NavigationLink(destination: DetailMealView(ingredientsObject: IngredientsObject(meal: meal))) {
                        MealRow(name: meal.name)
                    }
                }
            }.onDelete(perform: delete)

Not very pretty but it gets the job done until I find the real cause for the crash.

2条回答
家丑人穷心不美
2楼-- · 2020-05-24 06:28

How SwiftUI's ForEach appears to work is that after getting sent objectWillChange() it iterates over the collection it was previously given and the new collection it is given, and then diffs them. This only works properly for immutable collections, but Realm collections are mutable and live-updating. In addition, the objects in the collection also change, so the obvious workaround of copying the collection into an Array doesn't full work either.

The best workaround I've come up with is something like the following:

// helpers
struct ListKey {
    let id: String
    let index: Int
}
func keyedEnumeration<T: Object>(_ results: Results<T>) -> [ListKey] {
    return Array(results.value(forKey: "id").enumerated().map { ListKey(id: $0.1 as! String, index: $0.0) })
}

// in the body
ForEach(keyedEnumeration(self.userData.meals), id: \ListKey.id) { key in
    let meal = self.userData.meals[key.index]
    NavigationLink(destination: DetailMealView(ingredientsObject: IngredientsObject(meal: meal))) {
        MealRow(name: meal.name)
    }
}

The idea here is to extract the array of primary keys up front and give that to SwiftUI so that it can diff them without having to touch Realm, rather than trying to read from the "old" collection that's actually been updated.

A future version of Realm will have support for frozen collections/objects that'll be a better fit for the semantics that SwiftUI wants, but no ETA on that.

查看更多
贪生不怕死
3楼-- · 2020-05-24 06:29

I created a helper function based off Thomas's answer. This way, you can change all your ForEach references to ForEach:

public struct ForEachRealm<C: RandomAccessCollection, T: Object, Content: View>: View where C.Element == T, T: Identifiable, C.Index == Int {
    let results: C
    let fn: (Int, T) -> Content

    public init(_ results: C, @ViewBuilder fn: @escaping (T) -> Content) {
        self.results = results
        self.fn = { _, object in return fn(object) }
    }

    public init(_ results: C, @ViewBuilder fn: @escaping (Int, T) -> Content) {
        self.results = results
        self.fn = fn
    }

    public var body: some View {
        ForEach(keyedEnumeration(results)) { holder -> Content in
            return self.fn(holder.index, self.results[holder.index])
        }
    }

    // From https://stackoverflow.com/questions/57160790/index-out-of-bounds-when-using-realm-with-swiftui
    struct ListKey<T: Identifiable>: Identifiable {
        let id: T.ID
        let index: Int
    }

    func keyedEnumeration(_ results: C) -> [ListKey<T>] {
        return Array(results.enumerated().map { ListKey(id: $0.element.id, index: $0.offset) })
    }
}
查看更多
登录 后发表回答