SwiftUI: Understanding Core Data [Draft]

SwiftUI: Understanding Core Data [Draft]

When I started on mobile development I tried the three main flavors of it: Android Native, iOS Native and Flutter. Besides being an Apple costumer myself, the developer experience is one of the reasons why I decided to dive into iOS Native development. Apple delivers solutions to complex problems throughout it’s rich ecosystem of frameworks and APIs so developers can focus on what matters: solve people’s problems instead of computers’ problems.

Core Data is an object graph and persistence framework amongst the list of frameworks provided by Apple. It’s available on iOS since iPhone SDK 3.0. Yep, it’s really old and mature, which in software usually translates to “stability”. But equally, it’s starting to show its age when it comes to integrating the framework into new ones like SwiftUI.

Core Data has undoubtedly contributions to give to the hot topic around SwiftUI and MVVM since the introduction of a new property wrapper to integrate the framework into SwiftUI: @FetchRequest. Actually the framework has its caveats to integrate into Swift it self since the framework was built with Objective-C; Odptional has different meaning between the framework and the Swift language.

A Core Data sample app
Core Data sample app.

When experimenting with Core Data and SwiftUI, state management was one thing really mind-bending to me. Mixing an imperative framework with a declarative one feels really weird. Commits 195f451 and 57e5aa1 were me trying to deal with changes to Core Data’s NSManagedObject which conforms to ObservableObject not updating the view.