如何将RealmDB结果对象正确映射到SwiftUI列表?

时间:2019-10-25 20:06:53

标签: swift swiftui combine swiftui-list realm-database

我正在尝试在SwiftUI列表中显示realmdb查询的结果,但是在删除数据库对象时遇到了麻烦。

我正在尝试使用类似这样的东西:

final class DBData: ObservableObject{

let didChange = PassthroughSubject<DBData, Never>()

private var notificationTokens: [NotificationToken] = []
var events = try! Realm().objects(ADMEvent.self)
@Published var events: [ADMEvent] = []
init() {
    // Observe changes in the underlying model
    self.notificationTokens.append(posts.observe { _ in
        self.events = Array(self.posts)
        self.didChange.send(self)
    })
}
}

如果我在列表中显示项目,那会起作用,但是当我使用realm.deleteAll()时,应用程序崩溃了,因为它看起来像Swift UI的list实现正在尝试对列表进行区分,从而访问了现已失效的realm db对象。

关于堆栈溢出,有3或4个类似的问题,但是它们都以一种或多种方式过时了,或者可以工作,但在删除时仍然存在此问题。

谢谢!

2 个答案:

答案 0 :(得分:0)

领域对象是自动更新的,这就是为什么当您尝试按住已删除的对象时它们会崩溃的原因。不用给您发布主题Realm.Object而是将其映射到仅包含您需要使用的字段并使用该数组来驱动SwiftUI的结构。

struct Event: Identifiable {
  var id: String
  var name: String
  var date: Date
}

final class DBData: ObservableObject {
private var notificationTokens: [NotificationToken] = []
var events = try! Realm().objects(ADMEvent.self)
@Published var publishedEvents: [ADMEvent] = []
init() {
    // Observe changes in the underlying model
    self.notificationTokens.append(posts.observe { _ in
        self.publishedEvents = events.map { Event(id: $0.id, name: $0.name, date: $0.date)}
    })
}
}

答案 1 :(得分:0)

我喜欢这种方法!我只想把它放在那里,因为可接受的答案将无法编译并且存在多个问题:

@Published var publishedEvents: [ADMEvent] = []

应为:

@Published var publishedEvents: [Event] = []

 self.notificationTokens.append(posts.observe { _ in

应为:

 self.notificationTokens.append(events.observe { _ in

如此

final class DBData: ObservableObject {
    private var notificationTokens: [NotificationToken] = []
    var events = try! Realm().objects(ADMEvent.self)
    @Published var publishedEvents: [Event] = []

    init() {
      // Observe changes in the underlying model
      self.notificationTokens.append(events.observe { _ in
        self.publishedEvents = events.map { Event(id: $0.id, name: $0.name, date: $0.date)}
      })
    }
}