|
I am struggling a lot with this one. I am fetching a database with FetchRequest. Then I am displaying the results in NavigationView using ForEach to loop in NavigationLink with destination to data. The code is this:
This is what I have: list view screenshot What the code does is it displays every meal from core data which is okay, but how to group the meals by their date and display the list with date headers? |
|
hi, to do what you propose, you'll want to use this type of structure in the body using
where does the so you need a computed variable:
two items remain:
the second part is easy. if you have an array of meals consumed on the same day, you just have to report the day of any one of the meals, because they were all consumed on the same day. example:
as for the first part, start with a function that takes an array and forms it into a dictionary based on some grouping criterion -- in your case, by date (the month-day-year, without the actual time of day)
the keys of this dictionary are dates, the values in this dictionary are arrays of meals consumed on that date. you use this in the
that's just about everything, but you should probably sort the keys of the dictionary so that sections appears in the right order. hope that helps, DMG |
|
@delawareGuy and I were madly slinging code! His is excellent! 🏆 As @delaware shows, take your entire returned list of meal objects and group them by date. Then create section objects for each date. Within the section object, list the meals corresponding to that date.
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
I've been searching around for a while, and this is exactly what I'm looking for. I don't mean to hijack the thread (it hasn't been marked as answered yet), but I'm having a hard time porting the example given by @Obelix above to use Core Data, because I don't understand what pieces to change from the example. Am I correct in assuming if we're using Core Data we can ignore the entirety of the |
|
Hi @mradamw, Did you try to use a SectionedFetchRequest instead? This will allow you to group the meals by date an put the dates as a headers in every section. |
|
@CacereLucas I've tried using them before, but could never get the syntax right. I can make a new thread so as to not derail it, but any help would be greatly appreciated. |
|
Try this:
This should generate a list by grouping the items by date. For it to work properly you have to use the computed property that @Obelix suggest, so the sections ignore the time and only take into account the day and month. You have to put this as an extension of Food entity. (Remember to add the @objc before var to avoid a runtime error.)
Let me know if it works for you or if you have a problem. This code may vary depending on whether some Core Data attributes are optional or not, so let me know how it has worked for you. |
|
@CacereLucas thank you so much, that worked perfectly! I have a timeline view now! There were a few changes I made, laid out below:
I had to force unwrap the date, because it's an optional (but will always exist). Is there a better way to do this? The last request I have comes down to beefing up the formatting for If I had to take a guess, maybe something like:
|
|
@mradamw I'm very happy that it worked for you! :) In relation to force unwrappe the optionals in Core Data, the way I do it (but I am not entirely sure that it's the BEST way to make it), is manually generate the managed object subclasses from entities and if I'm sure that is not an optional, then I delete the "?" from the attribute. This way, the attribute doesn't bother me anymore with the optionals. But again, I don't know if it's the best way to do it. I don't know if I fully understand what you want to achieve with consumedDateAsString. Do you want the first two sections to say "Today" and "Yesterday" respectively and then the rest of the sections show the dates? |
|
@CacereLucas sorry for not replying sooner. I remember Paul showing something similar re the subclasses in one of his videos. As for the today/yesterday thing, you got it. I managed to make it work, here's my amended extension:
|
|
|
|
Sorry to reopen the thread but I'm stuck when using SectionedFetchRequest as suggested by delawaremathguy, If I use this :
How can I not display the section header if there is no meals in this section? I think my problem is that each section could have meals AND/OR drinks (for example), and when I want to display only meals, there is also header from drinks for this date... I can open a new thread if needed :) |
|
hi @Greetcha, it's been a while since i was in this thread ... but i can quickly answer your question:
if you use the Dictionary(grouping:) method to form the suggested i have not used hope that helps, DMG |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.