Hope averyone is well today?
I am having an issue with a Core Data date issue that has me going around a bend for the last few days.
I am currently working on my first app, and one one the main features is the ability to save a meeting on a specific date that should and I say should be listed under the date that has been selected when the meeting has been created.
I am able to save and retrieve the meeting entry to and from Core Data, but it is not listed under the correct date. I am hoping someone can help me figure out what I am missing(and I am missing something)
Sorry about that, on the main MeetingList view there is a view that comprises of a list view with an embedded date scroll view.
When I press the add meeting floating button it opens a new add meeting view to add and save a meeting on a specific date and time. Or supposed to save this instance to a specific date, say Jan 4th 2023. However I can date scroll the dates on the meeting list view, however the view does not change to show the scheduled meeting for a set date, it is showing all meetings for all days.
From what I can see the correct date is being saved into CoreData.
In your MeetingsListView the issue is that you are iterating over meeting (should be named meetings since it is an array) rather than the dateHolder.selectedMeeting. Similarly, I would recommend that you refactor and rename selectedMeeting to selectedMeetings (more than one meeting may be retrieved)
In DateScroller, as you move forward and backwards you need to refresh the data that populates the selectedMeeting(s) array so in DateHolder, remove the fileprivate keyword preceding the function declaration refreshMeetingItems then in DateScroller add a call to the refreshMeetingItems in each of the moveForward and moveBack() functions. ie:
dateHolder.refreshedMeetingItems(viewContext)
Just a note regarding the way you are injecting an .enviromentObject()
You have injected .environmentObject(dateHolder) in your Parent View and as such there is no need to inject it anywhere else since you can access it in any view that requires it by using:
@EnvironmentObject var dateHolder: DateHolder
I also noted that the deleteRecord() function needs to be modified so that the correct record in CoreData is removed since the seletedMeeting array that is being looped over is a subset of meeting
private func deleteRecord(indexSet: IndexSet) {
for index in indexSet {
print(index)
let selectedItem = dateHolder.selectedMeeting[index]
if let itemIndex = meeting.firstIndex(where: { $0.meetingDate == selectedItem.meetingDate}) {
viewContext.delete(meeting[itemIndex])
// And refresh selectedMeeting
dateHolder.refreshedMeetingItems(viewContext)
}
}
Also when you add a new meeting you also need to refresh the selectedMeeting array if the meeting you are adding is the current day otherwise you don’t see anything. That was doing my head in for a while.
If you want me to post the updated project then let me know otherwise I’ll leave it to you to make the necessary changes in your version. By the way, in the version I have here, I made a number of changes to the naming convention to make it easier for me to understand.
I’ve been through the code and made some notes here and there where I thought it might provide more clarity. Given that you have Source Control enabled on the project it will be obvious where I have tinkered with the code. Hope it all makes sense.
I have also got all your previews to work. I moved the files around a little in the Project Navigator panel on the left. Mainly for my own preference rather than considering that you might want to see the adjusted project. Hope you can still find everything.
Did you mean to call the ObservableObject DateHolder or was it supposed to be named DataHolder?
I placed that in it’s own folder named Observable Object since that makes it easy to find and groups the files in the Project Navigator.
I also grouped the Core Data files into a folder for the same reasons.