|
When the main view code gets large, it's convenient to create a new view file to work with. But I can't figure out how to transfer data from the main view to another child file (view) ContentView View
NestsSection View
Errors in NestsSection View: Cannot find 'nests' in scope Cannot find 'birds' in scope Is it possible to get this data from ContentView View ? |
|
|
|
Add a In your navigation link, set the destination to the child view, and pass the value from the parent view to the child view. |
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Thank you guys. Had to use both answers and they worked for me. I dubbed @FetchRequest and FetchedResults in NestsSection: View and NestBirdsList: View from ContentView: View And add @StateObject var nest = Nest() in in these views That is, it means that always need to duplicate @FetchRequest from main View? For some reason, it seems to me that this is not quite the right solution (albeit a working one). I think I should use @EnvironmentObject and the environmentObject() modifier somewhere. And in general, it's probably better to use the ViewModel in such projects? |
|
Just a small note
Excerpt From SwiftUI for Masterminds 3rd Edition 2022 J.D. Gauchat So @FetchRequest is not expensive operation as it will request only those items needed by view at given moment. |
|
Thanks @ygeras for the advice and explanation. And by the way, today I discovered that my method to share the view, although it turned out to be working, shows an error in the console, even though the app works.: CoreData: error: Failed to call designated initializer on NSManagedObject class 'Nest' If to use only ContentView that this error is not present. |
|
Your error is possibly because of your stateObject
"That is, it means that always need to duplicate @FetchRequest from main View?" No, you can keep the fetchrequest in the contentView and pass the data to the child with @StateObject like below
Then in your contentView you will pass the info like this:
This is I guess what @SwiftDevJournal meant. |
|
Yes, it looks like the error was due to
I replaced it with
An and the error is no longer shown in the console. At the same time, this was required only in NestBirdsList: View, in NestsSection: View I did not specify anything at all, only About I added Error:
|
|
|
|
It's in ContentView instead of
? This causes even more errors. Perhaps I added something superfluous somewhere or not there in the code of the entire application. But at least I was able to make the transition from ContentView to child views. Although @FetchRequest is required in every view (and this is probably wrong). |
|
Only in the child view, in order to solve your previous error in the ContentView |
|
This also does not fix the error: Cannot convert value of type 'FetchedResults<Nest>' to expected argument type 'FetchedResults<Nest>.Element' (aka 'Nest') In general, everything works so far if just use NestsSection() in ContentView but need @FetchRequest in NestsSection View |
|
|
|
|
|
|
|
Hmm..try this in the NestSection View
Paul is calling one book, you call the list of "books". |
|
If used And there was an idea not to use at all @FetchRequest and FetchedResults in child View, and "pull" data about the request and the result from the parent view. Or is it not possible... |
|
It seems to work like this without @FetchRequest:
|
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.