1

I am running into a problem trying to use an in memory realm configuration with previews. When the preview executes it returns an error. The diagnostics gives the following error:

MessageError: Connection interrupted

I have the following class defined to mock the realm data.

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)")
         }
      }
   }
}

In the view we are using the @ObservedResults to get the categories from the Realm.

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)
} 

The preview section is as follows:

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)
      }
   }
}

Any help would be most appreciated.

Cosmtar
  • 516
  • 5
  • 14
  • Preview is really not for this - you should use static local data to *preview* design of UI. All real testing/flows should be done on Simulator and/or real device. – Asperi Feb 04 '22 at 14:00
  • @Asperi - I use static local data where I can, but since the variable for the view is of @ObservedResults how would you represent that? If this cannot be represented in the preview then all of the UI design would have to be in the simulator which is counter intuitive to the purpose of previews. – Cosmtar Feb 04 '22 at 15:19
  • It looks like you mixed buisiness logic with view model so have troubles. Just separate them and you have pure view model level with data to be viewed and engine level to fetch those data. In such case to preview you will just need to set view model with injected static data. If view model would be represented by protocol then you can mock it easily for preview. – Asperi Feb 04 '22 at 15:33
  • I think you need further troubleshooting; does this code within the simulator? At what pointing time are you receiving that error? If you comment out all of the realm code, does it run? Have you verified that code `ForEach(categories) { category in` is accessing the correct Realm data. Please update the question with troubleshooting details so we can take a look. – Jay Feb 04 '22 at 17:11
  • Thanks @Jay. Yes, notwithstanding the preview not working, when I run the code in the simulator the ForEach(categories) does return the data in the on disk Realm and no errors are trapped. This only seems to be an issue in previews when I go to setup the in memory Realm and populate it. Let me know of any specifics you'd like beyond that! – Cosmtar Feb 04 '22 at 19:03

1 Answers1

1

On the main content view I call the PropertyListView with an environment setting realmConfiguration and pass that into the PropertyListView. So I changed the PreviewProvider to pass simply the in-memory Realm configuration and then everything seems to work just fine.

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)
     }
  }

}

With Realm changing almost every other day it is like walking on quicksand. It is a wonderful DB for mobile development because they are constantly making it better, but boy you really have to stay on top of the changes.

Cosmtar
  • 516
  • 5
  • 14
  • can you explain at which point does `static var previewRealm: Realm` is getting called so the preview data added? it is not clear from your answer. Thanks :) – Duy Anh Jun 10 '22 at 07:24