Posts

Post marked as solved
4 Replies
2.2k Views
I have a view that is pulling the context from the environment using the latest SwiftUI CoreData template (i.e. there is a preview/in-memory context vs the persistent context). I also have an @ObservableObject class that is fetching objects based on the predicates passed in (think dynamic filtering). A random element from these fetched results are then displayed back in the view (i.e. I do not need this ObservableObject class to be a view itself). However, there is an interesting "issue" where I cannot instantiate my @ObservedObject because the property initializers are run before "self" is available and I need to pass it the NSManagedObjectContext. The only way I can think to get around this is to create the ObservableObject class outside of the view and pass it in the view's initializer. However, this isn't completely desirable as I would prefer this data be completely private to the view as other views do not need to know about its existence. I also need it to be an ObserableObject so that the filters can change and it be reflected back in the view observing it. Am I using the wrong tool or thinking about this wrong? class Filter: ObservableObject { @Published var someObjects: [Objects] = [] /* Need to instantiate with the context so objects can be fetched */ private let context: NSManagedObjectContext } struct ContentView: View {     @Environment(\.managedObjectContext) var context 	 /* Cannot initialize here as context isn't available */     @ObservedObject var filter = Filter(context: context) }
Posted
by bknope.
Last updated
.
Post not yet marked as solved
0 Replies
329 Views
I have a view that will display available genres. Genre is a NSManagedObject containing the title of the genre. I am trying to create a Samples class that provides sample data to be used in the PreviewProvider. However, the Genre returned from the Samples class has its genre string set to nil. For the life of me...I cannot figure out why this is happening: class Samples {     static var sampleGenre: Genre {         let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)         let genre = Genre(context: context)         genre.genre = "Horror"         return genre     } } Then in the PreviewProvider: struct GenreRow_Previews: PreviewProvider {     static var previews: some View {         GenreRow(genre: Samples.sampleGenre)     } } The GenreRow is simple: struct GenreRow: View {    var genre: Genre     var body: some View {         HStack {             Text(genre.genre ?? "Unknown genre")         }         .padding()     } } The text is set to "Unknown genre" even though one has previously been set. Any guidance would be appreciate. How are people previewing views with NSManagedObjects?
Posted
by bknope.
Last updated
.
Post marked as solved
2 Replies
5.4k Views
In Xcode 12: I have a protocol: protocol Parameter { } I then have several structs conforming to this protocol: struct Temperature: Parameter { } ... I have a ParameterView that takes a parameter and builds out a view: struct ParameterView: View { @Binding var parameter: Parameter ... } Then I have a ParametersView (multiple parameters together now) that uses multiple parameters: struct ParametersView: View { @Binding var temperature: Temperature  var body: some View {   VStack(alignment: .leading) { ParameterView(parameter: $temperature) ... } } } The compiler does not like this: Cannot convert value of type 'Binding<Temperature>' to expected argument type 'Binding<Parameter>' I must be violating something, but being a little rusty, I cannot determine if this is expected behavior or a compiler problem or something I completely messed up. Any assistance would be appreciated
Posted
by bknope.
Last updated
.