1

I am new to Swift and am building a nutrient tracking application for a project. I need the users foods to be saved to the day that they add them to their food diary section which displays the foods added to each meal.

So the user adds an apple for example, and this would be saved to today, and once the day passes, the displayed data is stored to the date.

I currently have the food items saving with a Date using CoreData and this is displayed in a list:

    func saveBreakfast() {
        
        
        let newBreakfastItem = BreakfastItem(context: self.moc)
        newBreakfastItem.id = UUID()
        newBreakfastItem.name = self.item.name
        newBreakfastItem.calories = Int32(self.totalCalories)
        newBreakfastItem.carbs = Int32(self.totalCarbs)
        newBreakfastItem.protein = Int32(self.totalProtein)
        newBreakfastItem.fat = Int32(self.totalFats)
        newBreakfastItem.date = self.dateAdded

        
        
        
        do {
        if self.mocB.hasChanges { // saves only if changes are made
        try? self.mocB.save()
            
            }
            
        }
}

ForEach(self.BreakfastItems, id: \.id) { newBreakfastItems in
                        
                       HStack{                            
                        
                            Text(newBreakfastItems.name ?? "Unknown")
                                 .font(.headline)

                             .font(.headline)
                    
                        
            
                            HStack {
                             
                                
                                Text("Cal: \(Int32(newBreakfastItems.calories))")
                                    .fontWeight(.light)
                                    .foregroundColor(Color("Green Font"))

                                Text("F: \(Int32(newBreakfastItems.fat))")
                                    .fontWeight(.ultraLight)

                                Text("P: \(Int32(newBreakfastItems.protein))")
                                    .fontWeight(.ultraLight)

                                Text("C: \(Int32(newBreakfastItems.carbs))")
                                    .fontWeight(.ultraLight)

However, Im not sure how to view the food saved to past days, as currently the app is just adding to the same list all the time, regardless of the day its saved on.

Would some form of ForEach Date statement work to cycle through views based on the day selected from the calendar?

Thanks in advance!

Noonan _12
  • 15
  • 4

1 Answers1

0

If you want to fetch the items for a special day, you could just use a predicate to your FetchRequest with a specific date. However, then you might have many fetch requests for each day. (not sure how you display it in your app)

Second approach, you can just fetch all your items, and filter them based on a date and show them. Here is an example filtering your items in the ForEach.

ForEach(self.BreakfastItems.filter { 
  $0.date == Date()
}, id: \.id) { newBreakfastItems in

This will only display items, where the date is equals the current Date(). You might need to check same date only, regardless of the time.

You can add more filters based on the current date you want to show.

davidev
  • 5,693
  • 3
  • 7
  • 32
  • Thanks for the reply! That second options looks best as i want to specify the date using a user selected Date from a DatePicker. However this isnt working for some reason, I think it might be because the selected Date and the $0.date are formatted differently? Right now i have @State var selectedDate : Date and ForEach(self.BreakfastItems.filter { $0.date == selectedDate }, id: \.id) { newBreakfastItems in, any idea why this isnt working? – Noonan _12 Aug 20 '20 at 14:34
  • @Noonan_12 The approach is correct. However, the date will always save the time aswell. Hence, the dates are not equals. Check this question, how to compare dates without the time and you will have the solution. https://stackoverflow.com/questions/24577087/comparing-nsdates-without-time-component – davidev Aug 20 '20 at 21:02
  • that was what i was thinking, thanks for your help! – Noonan _12 Aug 21 '20 at 12:09