The first thing I noticed is that you're using a UUID for RandomNumberEntity
. That in itself is not a problem, except that the UUID is always changing because you create it using UUID()
in your implementation. An entity needs to have a stable identifier so that the same entity from your app is always represented by the same UUID value.
The second thing is that I don't see an EntityQuery
here. You need to implement an EntityQuery
, and it needs to implement the entities(for:)
method at a minimum. Borrowing an example from the documentation , that might look like this:
struct MyPhotoQuery: any EntityQuery {
func entities(for identifiers: [UUID]) async throws -> [MyPhoto] {
myPhotoStore.filter { ids.contains($0.id) }
}
Notice that the type of the identifiers
parameter is a UUID, which is convenient to match your test project. The system will call that method with an identifiable value (such as your UUID), and your query needs to return the entity for that value. This connects to my first point about using a stable identifier — the identifiers that you create for your random numbers then must return that random number entity if the system asks you for that entity again by its identifier.
This might make a bit more sense if you implement your test project using Int
as your Identifiable
value rather than UUID
, since your test project is generating random numbers. The id
property can be the same random number integer as the value that you boxed into the RandomNumberEntity
type, and then the query would be asking you for an entity matching an integer, which means its easy for you to always return the same integer with the same stable id
.
— Ed Ford, DTS Engineer