Monday, 2 December 2019

SwiftUI and Core Data — Publishers not working and context not detecting changes

I have an application in which I use SwiftUI and Core Data. In the Core Data datamodel, I have an Entity, Record, which has various attributes, two of which are of a custom class type. These classes both contain a property which is an array of yet another custom class type. Some of properties in all of the aforementioned class types are prefixed with the @Published property wrapper, so that the view will update when these properties change, along with the context. Rather unfortunately, this does not seem to be the case — and I can't for the life of me work out why! I have tried to work around this by having a custom delegate to manually call objectWillChange.send(), but this doesn't feel very clean, nor does it register changes with the context.

The application can be found here, the relevant data model classes are in this directory, and the relevant view classes are ContentView.swift and RecordDetailView.swift (I have commented the [SwiftUIView]_Previews structs because I'm not quite sure how to get the previews to work with the Core Data container). I won't post any code-snippets here, as they are somewhat dependent on each other and there is too much to form a concise question.

On another note, there are also several really horrible UI glitches which I do not understand, for example, sometimes when tapping on a RecordThumbnailView from a ContentView it will do nothing, but then when tapping on a different RecordThumbnailView it will suddenly push all of the RecordDetailViews which correspond to all of the unresponsive RecordThumbnailViews. In addition, when performing this same segue, the ChartViewControllerView will jolt upwards very quickly, and will be hidden behind the navigation bar.

If you know of any ways to overcome these issues, I'd appreciate the assistance — I've been staring at these problems for several days!



from SwiftUI and Core Data — Publishers not working and context not detecting changes

No comments:

Post a Comment