|
Assuming I have a to many relatioship Parent -> Children, is there a way to make a query for Parent but sort the array based on Children property? or how could I achieve that. No much tutorials around yet. |
|
Thanks @magna, I have read that but I can't figure out how to achive the following: in the main view I use @Query to fetch an array of parents with a predicate (there will be only one parent with this predicate). Then pass this parent to another view by a NavigationLink. From here when using ForEach($parent.children) there is not problem, but I would need that the array of children is sorted. If I try to sort it the bindable would no longer work. I have tried different ways but it seems I can't find the right one. How can I get Children sorted when fetching from Parent or what workaround |
|
@Obelix thanks for pointing that out, I had found it anyway. This is the correct link: https://www.hackingwithswift.com/quick-start/swiftdata/ |
|
Markdown Link SyntaxTry this technique: Click the link icon while composing your message. It looks like three chain links. Enter a clever text description between the [] brackets. Enter the web address between the parentheses. ()
In markdown, this resolves to: This gives your link a cleaner look in forum responses. Keep Coding! |
|
Thanks @Obelix I'll use the Markdown link syntax next time... my bad. Regarding my problem. Let's assume I have an array of family with properties name and origin
and each family has an array of four members called
Based on the following Fetch:
in the EditFamily view the outcome woul be: Family Name: Bennett Member1 name: Laura Member2 name: Henry Member3 name: Susan Member4 name: Adam But I want them ordered: Member1 name: Adam Member2 name: Henry Member3 name: Laura Member4 name: Susan I can't understand how to Query from Family the relationship People sorted by name. P.S.: I am aware I could use computed property with get and set to return the array sorted like with Core Data, just curious to see if that can be done |
|
I am fighting similar challenges when working with parent and children relationships and @Queries. I believe there is currently no way to order the result by children properties when querying the parents. However, what you could try is, that you are doing a second query filtering for all children where children parent is a specific parent and ordering the result as you prefer. The computed property should also work but I believe this gets difficult when querying a lot of objects regarding performance. |
|
@rockeby would like some order to his family tree:
I used some code from another example program I was working. Perhaps this example can help you understand a one-to-many relationship and how you can select objects from It's a long example. But I've annotated with lot o'comments. Lumos!Think of Hogwarts. Each House in Hogwarts has a number of student wizards. Each Wizard is in one house, but each house may have several Wizards. House and Wizard Models
House ViewThe answer to your question (above) is in the
Helper MethodsThis is an extension to the view code. It contains a few helper methods.
Subview StructsThese are subviews used in the This level of organization isn't necessary for small See -> Organizing your Views
Keep Coding! |
|
Thank you very much @Thensel and @Obelix. @Obelix I think that will work with my idea. I will give it a go as soon as I can. Thanks again for taking your time to find a solution, cheers ! |
|
@Obelix, question on your (incredible) example above. In your House and Wizard Models, you include a reference to House in Wizard, which @twostraws does not do in his SwiftData project (there, Destination references Sight and adds @Relationship, but Sight has no mention of Destination). Is there a reason you do this? Perhaps unrelated, but you also initialize wizard in house (and vice versa), which isn't something done in the sample project either. A few of us are struggling to delete the 'relationship' items in the SwiftData project challenge...wondering if you're onto something here. https://www.hackingwithswift.com/forums/books/hacking-with-swiftdata-delete-a-sight/24119 |
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.