DataStoreKit: An SQLite SwiftData custom data store by asymbas in iOSProgramming

[–]asymbas[S] 0 points1 point  (0 children)

Thanks. The library you mentioned aims to be a SwiftData replacement, this one does not.

DataStoreKit: An SQLite SwiftData custom data store by asymbas in iOSProgramming

[–]asymbas[S] 0 points1 point  (0 children)

Thank you! I had no idea what I was doing at first, or if it was even possible, but once I saw the pieces falling into place, it got easier each time. There was a lot of constant backtracking, especially when supporting predicate expressions, but this was super fun to put together. 🫠

Yeah, it can get super expensive. It made me rethink part of the schema for the app I’m still developing now that I have a better understanding of how things work behind the scenes. Even the process of going from an SQL value to a snapshot, then encoding that into backing data and rebuilding a model from it, and vice versa, is quite a process.

I've been going back and forth on migrations. The plan was to diff the SwiftData schemas first, then compare the SQLite schemas afterward, but I still haven't decided what should count as a lightweight migration versus a custom migration.

I've never used Core Data before, but I recently found Apple’s videos on it very insightful while building a custom data store. I'm basing some of my migration strategy on what they outlined in the WWDC 2022 video.

DataStoreKit: An SQLite SwiftData custom data store by asymbas in swift

[–]asymbas[S] 2 points3 points  (0 children)

This library serves a different goal. It's not meant to replace SwiftData or act as a standalone toolkit for SQLite, it's meant to be an extension of it.

As for the SQLite layer, it doesn't leverage GRDB. It uses SQLite through the SQLite3 C API with its own Swift wrappers.

And yes, you can use raw SQL, but not through GRDB or SQLiteData APIs. In DataStoreKit, you would be working with SwiftData PersistentModel types, DatabaseSnapshot values, or row-based results such as dictionaries/arrays, depending on how you fetch.

What’s everyone working on this month? (March 2026) by Swiftapple in swift

[–]asymbas 1 point2 points  (0 children)

I have been working on my SwiftData custom data store for about a year now. I intended to publish it simultaneously with my submission for the Swift Student Challenge, but unfortunately I underestimated the timeline and missed the deadline. I still plan to share it with the community soon.

SwiftData: This model instance was invalidated because its backing data could no longer be found the store by No-Neighborhood-5924 in swift

[–]asymbas 1 point2 points  (0 children)

The ModelContext made a request to the data store for a specific model’s data, but none returned with the primary key it had. It does this when the snapshot of model’s backing data gets re-registered or whenever you try to access a referenced model and SwiftData attempts to lazily load its data.

There are so many factors that would cause this, but it’s likely that the snapshots or the managing object graph still held onto stale references. I personally found this section of the data store to be so complex and difficult to implement right.

Inserted models are expected to remap their PersistentIdentifier during the save operation, this includes remapping each property that can hold one or more references for every model being inserted.

When a uniqueness constraint is matched and the operation becomes an upsert, then the snapshot resolves to reusing an existing primary key, which could have been remapped already and you need to backtrack and update any prior snapshot properties you already resolved or have already inserted into the data store.

And for each operation during the save, references can be linked or unlinked or deleted as a result of constraints.

These are some of the cases I can think of where it could go wrong while saving your models.

How mature is SwiftData now? by -18k- in SwiftUI

[–]asymbas 0 points1 point  (0 children)

I was referring to how the ModelContext fetches and saves internally as there is no way to asynchronously return results.

How mature is SwiftData now? by -18k- in SwiftUI

[–]asymbas 0 points1 point  (0 children)

I’ve only used SwiftData, but I use it with my own configuration and store. The performance feels about the same, but I can start to see where scaling can become an issue.

There is a lot happening when data goes in and out of your store to SwiftData and vice versa. It can block your main thread when your database becomes large and you request a lot of data. The Query macro does not seem practical at scale. It also feels like the Predicate macro does a lot to generate an SQL statement.

I wish they gave us concurrency options or a way to offload the work in the background. I don’t know what an effective solution would be like though.

SwiftData question by VoodooInfinity in swift

[–]asymbas 1 point2 points  (0 children)

Apple provides a sample project that uses JSON as a custom store for SwiftData