Reduce/replace loops in my conditional checks ? [CoreData]

I am looking to integrate my code with CoreData. Presently I have the following of which allows a worker to check their requirements against an advert e.g. if the worker needs x3 sponges to clean a bathroom, then the advert must provide those sponges, but the worker can only work on set days:

Code Block swift
var cdExample: [CleaningRota] = [bathroomMonday, kitchenFriday] //
[Full sample code](https://developer.apple.com/forums/content/attachment/648c2166-46f3-44a3-bfc7-5e36f3d38721){: .log-attachment}
CORE DATA EXAMPLE
let personAvailable: [Person] = [kath, mavis]
//CORE DATA EXAMPLE : WORK ON OFFER
for work in cdExample {
//WORK TOOLS PROVIDED ON SITE
for toolsAvailable in work.tool {
//PERSONS AVAILABLE TO WORK
for person in personAvailable {
//PERSONAL REQUEST : SPECIFIC WORK DAY
if person.avilability == work.day {
//CHECK PERSON TOOL REQUIREMENTS FOR JOB
for personRequirement in person.materialsNeeded {
//CYCLE THROUGH TOOLS WORKER NEEDS ON SITE
if personRequirement.tool == toolsAvailable.name {
//CONFIRM TOOLS WORKER NEEDS ARE FULLY PROVIDED
//AVOID CHECKING SAME STOCK TWICE IF CONDITION PREVIOUSLY MET
if personRequirement.quantity <= toolsAvailable.stock && personRequirement.met == false {
print(">>0> \(person.name)")
print(">>1> \(personRequirement.tool) : \(personRequirement.quantity)")
print(">>2> \(toolsAvailable.name) : \(toolsAvailable.stock)")
personRequirement.met = true
person.materialID.append(toolsAvailable.id)
}
}
}
}
}
}
}

As the adverts and requirements pile up this kind of system will not scale very quickly. I would like to know if there is a better way to model this data, or to check conditions.

I plan to pull the cdExample from CoreData, and am unfamiliar with using it. Being a proprietary Apple Framework I do not know if anyone will help me here, but if not then how best to achieve what I want without a pyramid of doom (which I don't mind), is there a better way please?

I also need to remove the stock, which I ran into trouble with as I had too many loops and hit a bug in Xcode, so I think I'm doing something wrong with all of these loops ?
Answered by Claude31 in 658736022
A limited simplification is to use where clause:

replace
Code Block
for person in personAvailable {
//PERSONAL REQUEST : SPECIFIC WORK DAY
if person.avilability == work.day {

with
Code Block
for person in personAvailable where person.avilability == work.day {
//PERSONAL REQUEST : SPECIFIC WORK DAY

So that would allow to write more compact code:
Code Block
for person in personAvailable where person.avilability == work.day {
for personRequirement in person.materialsNeeded where personRequirement.tool == toolsAvailable.name && personRequirement.quantity <= toolsAvailable.stock && !personRequirement.met {

that replaces
Code Block
for person in personAvailable {
if person.avilability == work.day {
for personRequirement in person.materialsNeeded {
if personRequirement.tool == toolsAvailable.name {
if personRequirement.quantity <= toolsAvailable.stock && personRequirement.met == false {

But I fear you have to go through the loops anyway.
Accepted Answer
A limited simplification is to use where clause:

replace
Code Block
for person in personAvailable {
//PERSONAL REQUEST : SPECIFIC WORK DAY
if person.avilability == work.day {

with
Code Block
for person in personAvailable where person.avilability == work.day {
//PERSONAL REQUEST : SPECIFIC WORK DAY

So that would allow to write more compact code:
Code Block
for person in personAvailable where person.avilability == work.day {
for personRequirement in person.materialsNeeded where personRequirement.tool == toolsAvailable.name && personRequirement.quantity <= toolsAvailable.stock && !personRequirement.met {

that replaces
Code Block
for person in personAvailable {
if person.avilability == work.day {
for personRequirement in person.materialsNeeded {
if personRequirement.tool == toolsAvailable.name {
if personRequirement.quantity <= toolsAvailable.stock && personRequirement.met == false {

But I fear you have to go through the loops anyway.
Reduce/replace loops in my conditional checks ? [CoreData]
 
 
Q