in my ios swift application I have a database using Core Data.
It has many entities, all entities have an integer field called syncStatus
. it can be 0, 1, or 2
.
On startup, I want to loop through ALL the entities that have syncStatus = 1
and change it to 0
Is there a way to do it without fetching each type alone and changing it?
So what I want is:
- fetch
ALL
entities withsyncStatus = 1
Loop
through them and setsyncStatus = 0
Currently I'm doing them one by one:
- fetch
UserEntities
withsyncStatus = 1
- Loop through them and set
syncStatus = 0
- fetch
countryEntities
withsyncStatus = 1
- Loop through them and set
syncStatus = 0
- Do the same for every entity one by one
code:
let allUsers = context?.fetch(FetchRequest<UserEntity>().filtered(with: "syncStatus", equalTo: "1"))
let allCountries = context?.fetch(FetchRequest<CountryEntity>().filtered(with: "syncStatus", equalTo: "1"))
.
.
.
I'm just trying to find a generic approach, in case later we add another entity/table we don't have to come back to this code and add it here also.