I'm trying to implement a custom data store for SwiftData. I've learned a bunch from JSONStore and ProtobufStore.

I'm confused by the DataStoreSnapshot initializer:

init(from: any BackingData, relatedBackingDatas: inout [PersistentIdentifier : any BackingData])

It takes any BackingData that represents the model with data. In order to query the model for data, I must know the model type and invoke some BackingData function, e.g.:

func getValue<Value>(forKey: KeyPath<Self.Model, Value>) -> Value where Value : PersistentModel

PROBLEM: I don’t want to couple my snapshot type to my model types.

Q: How can I avoid tight coupling, and instead have a single MySnapshot implementation that can interact with all of my model types without explicit customization for every known model type?

Apple provides a DefaultSnapshot implementation that is not tied to model types, yet it extracts data from models. How does it do that without knowing model types? Can I do the same? How?

user2962393's user avatar

2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.