0

我在尝试使用带有预览的内存领域配置时遇到了问题。当预览执行时,它会返回一个错误。诊断给出以下错误:

消息错误:连接中断

我定义了以下类来模拟领域数据。

class MockRealms {
   static var previewRealm: Realm {
      get {
         var realm: Realm
         let identifier = "previewRealm"
         let config = Realm.Configuration(inMemoryIdentifier: identifier)
         do {
            realm = try Realm(configuration: config)
            try realm.write {
               for category in Categories.allCases {
                  _ = Category(name: category.rawValue)
               }
            }
            try realm.write {
               _ = Settings(type: .fixed, int: 3.375, dp: 25.0, term: 30)
            }
            return realm
         } catch let error {
            fatalError("Error: \(error.localizedDescription)")
         }
      }
   }
}

在视图中,我们使用@ObservedResults 从领域中获取类别。

struct PropertyListView: View {
   @ObservedResults(Category.self) var categories
   @Binding var showInfo: Bool
   @Binding var showInfoButton: Bool
   @Binding var showGuide: Bool
   @Binding var showGuideButtton: Bool

   var body: some View {
      NavigationView {
         ScrollView {
            VStack {
               ForEach(categories) { category in
                  PropertyCategoryView(category: category)
               }
            }
         }
      }
      .navigationBarTitle("Property List")
      .navigationBarBackButtonHidden(false)
   }
   .navigationViewStyle(.stack)
} 

预览部分如下:

struct PropertyListView_Previews: PreviewProvider {
   static var previews: some View {
      Group {
         PropertyListView(showInfo: .constant(false), showInfoButton:
            .constant(true), showGuide: .constant(false), showGuideButton:
            .constant(false))
            .environment(\.realm, MockRealms.previewRealm)
         PropertyListView(showInfo: .constant(false), showInfoButton:
            .constant(true), showGuide: .constant(false), showGuideButton:
            .constant(false))
            .environment(\.realm, MockRealms.previewRealm)
            .preferredColorScheme(.dark)
      }
   }
}

非常感激任何的帮助。

4

1 回答 1

0

在主要内容视图中,我PropertyListView使用环境设置调用并将其realmConfiguration传递到 PropertyListView。因此,我将其更改PreviewProvider为仅通过内存中的 Realm 配置,然后一切似乎都运行良好。

struct PropertyListView_Previews: PreviewProvider {
   static var previews: some View {
      Group {
         PropertyListView(showInfo: .constant(false), showInfoButton:
           .constant(true), showGuide: .constant(false), showGuideButton:
           .constant(false))
           .environment(\.realmConfiguration, Realm.Configuration(inMemoryIdentifier: "previewRealm", schemaVersion: 1))
        PropertyListView(showInfo: .constant(false), showInfoButton:
           .constant(true), showGuide: .constant(false), showGuideButton:
           .constant(false))
           .environment(\.realmConfiguration, Realm.Configuration(inMemoryIdentifier: "previewRealm", schemaVersion: 1))
           .preferredColorScheme(.dark)
     }
  }

}

随着 Realm 几乎每隔一天发生变化,这就像在流沙上行走。对于移动开发来说,这是一个很棒的数据库,因为他们一直在使它变得更好,但是男孩你真的必须掌握这些变化。

于 2022-02-10T13:06:28.093 回答