Learning SwiftData

New in Xcode 15 - SwiftData

The new Apple framework for DataBase integration is a wonderful 1st release.  SwiftData is still in its growth phase - yes, you can start to use it and will find it easy for simple projects that require DB storage.

Here are two very similar learning paths for SwiftData (in the abstract sense).

I feel that's Paul's approach is straight to the point and focuses just upon SwiftData - a simple example App.  Where as Stewart's App is more robust and covers related topics - done well it just needs a bit of polish and a catchy title for the App Store.

Missing: Shared & Public

I was disappointed to find out (via reading problems) that this release of SwiftData does NOT support the CloudKit Shared & Public DataBases.  That puts a hitch in my plans for my next app.  But given it takes me a long time to create a simple App - maybe I can start and Apple will deliver just in time...

You have to use Core Data if you need these features of CloudKit in 2023/24.


MyBooks - Stewart Lynch

Just looking at the two finished project's listings will show us that Stweart's MyBooks project covers more ground and delivers a more robust starting point.

Stweart starts from the position of some experience with SwiftUI.  And launches right into designing the project's model - using a CRUD orientation.

Series Covers:

A git hub repo:  StewartLynch/MyBooks

SQLite Browser

This tool recommended by Stewart is great for viewing the underlying DB tables and records.

DB Browser for SQLite

The Official home of the DB Browser for SQLite


Paul's iTour Starter App

I have learned lots of Swift concepts from Paul Hudson (Hacking with Swift) and have purchased many of his books over the years.  His learning path is a bit shorter and more focused upon just SwiftData.  The iTour app - I renamed and typed in along with his video - a great practice for learning.


A git hub repo:  twostraws/iTour


SwiftData by Example - tutorial for Swift devs


SwiftData Setup

Stewart's SwiftData Setup method:  


You MUST be a registered Apple Developer.

Project  >  Signing & Capabilities


Then add the iCloud capability to the project.  Click the "+" button on the Capabilities tab - then search for iCloud - Double Click to add the capability.

Select the CloudKit checkbox and copy/paste your bundle ID into a new container.  Click the "+" button to add a new CloudKit container up on Apple's servers.

I added the container:  "iCloud.com.LifeWorksIQ.Equity-Curve"

I don't know if it is required to prepend the string "iCloud." or if that's just the convention.  But you use your bundle identifier for this container name.




Lastly you must add the Background Push Notification capability. Click the "+" button on the Capabilities tab - then search for Background Modes - Double Click to add the capability.  Inside the Background Modes - select the Remote Notification check box.





CloudKit requires all properties to be optional or have default values (not in the init() but in the declaration.  All relationships must be optional.  You cannot have @Unique property.


Data that was created before the CloudKit store was created will be lost.


Bezel - hyper realistic simulator.

Stewart recommends Bezel so for $30 you get all this.

Show your iPhone

on your Mac

Bezel is the easiest way to view, present and record an iPhone.


Some common start-up problems

You might get some errors in the console.  A common one on the simulator is the problem of not being logged into iCloud on the simulator device.  Switch to the Setting App and log in.

Making Relationships Obvious

I've started a SwiftData coding convention of appending "List" to my relationship nouns, to make the property more obvious.  What do you think about this?

Didn't Java try this OR Mapping

Yeah, way back in the early 2000s the Java Community Process got excited about Object Relational DB Mapping (JSR 12 Java Data Objects).  I was working in the Java world back in those days... it didn't work out too well (in my opinion).  Wonder why?  What's different about Apple's approach in Swift?


One article on the Oracle site from 2005 starts off with why it is so difficult to make ORM work.  "The task of persisting Java technology objects to any relational database is challenging because it involves serializing the hierarchically structured Java objects to a tabular-structured database and vice versa. This challenge is unique because of the need to map Java technology objects to database columns and records in a way that is optimized for both speed and efficiency. The Java Data Objects (JDO) specification,  Java Specification Request (JSR) 12, defines an API for a standard way to transparently persist plain Java technology object and database access. Using JDO, Java application developers can write code to access the underlying data store without using any database-specific code. JDO is designed to work in multiple tiers of an enterprise architecture including the Java Platform, Standard Edition (Java SE, formerly known as J2SE), web tier, and application servers."

One problem with Java - they fell in love with abbreviations, so it quickly creates an IN/Out group mentality.

SwiftData Mastery
in SwiftUI

Big Mountain Studio has a great book on mastering the SwiftData framework - I just bought it and started reading.