CoreData checking conditions met with lots of loops.. refactor advice

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:

class CleaningRota {

    var day: String
    var room: String
    var tool: [CleaningTool]
    
    init(day: String, room: String, tool: [CleaningTool]) {
        
        self.day = day
        self.room = room
        self.tool = tool
    }
}


class CleaningTool {
    
    var name: String
    var cost: Int16
    var stock: Int16
    var ok: Bool
    var id: UUID
    
    init(name: String, cost: Int16, stock: Int16, ok: Bool = false, id: UUID) {
        
        self.name = name
        self.cost = cost
        self.stock = stock
        self.ok = ok
        self.id = id
    }
}


class Person {
    
    var name: String
    var avilability: String
    var specialty: String
    var materialsNeeded: [PersonRequirements]
    var materialID: [UUID]
    
    init(name: String, avilability: String, specialty: String, materialsNeeded: [PersonRequirements], materialID: [UUID]) {
        
        self.name = name
        self.avilability = avilability
        self.specialty = specialty
        self.materialsNeeded = materialsNeeded
        self.materialID = materialID
    }
}



class PersonRequirements {
    
    var tool: String
    var quantity: Int16
    var met: Bool
    
    init(tool: String, quantity: Int16, met: Bool = false) {
        
        self.tool = tool
        self.quantity = quantity
        self.met = met
    }
}



    //TOOLS IN STOCK (ON SITE)
    let creamWhite = CleaningTool(name: "Cream White",
                                  cost: 1,
                                  stock: 7,
                                  id: UUID())
    
    let creamBrown = CleaningTool(name: "Cream Brown",
                                  cost: 2,
                                  stock: 13,
                                  id: UUID())
    
    let spongeYellow = CleaningTool(name: "Sponge Yellow",
                                    cost: 4,
                                    stock: 15,
                                    id: UUID())
    
    let spongeRed = CleaningTool(name: "Sponge Red",
                                 cost: 9,
                                 stock: 3,
                                 id: UUID())
    
    let bleachMedium = CleaningTool(name: "Bleach Medium",
                                    cost: 3,
                                    stock: 4,
                                    id: UUID())
    

    //DATES AVAILABLE FOR WORK (ROTA)
    let bathroomMonday = CleaningRota(day: "Monday",
                                      room: "Bathroom",
                                      tool: [creamWhite, spongeRed, bleachMedium])
    
    let kitchenFriday = CleaningRota(day: "Friday",
                                     room: "Kitchen",
                                     tool: [creamBrown, spongeRed])
    
    
    
    //PERSON (REQUIREMENTS)
    let requirement1 = PersonRequirements(tool: "Cream White",
                                          quantity: 6)

    let requirement2 = PersonRequirements(tool: "Sponge Red",
                                          quantity: 3)

    let requirement3 = PersonRequirements(tool: "Bleach Harsh",
                                          quantity: 1)
    
    
    //PERSON (CLEANERS)
    let kath = Person(name: "Katherine",
                      avilability: "Monday",
                      specialty: "Bathroom",
                      materialsNeeded: [requirement1, requirement2],
                      materialID: [])
    
    let mavis = Person(name: "Mavis",
                       avilability: "Friday",
                       specialty: "Kitchen",
                       materialsNeeded: [requirement1, requirement3],
                       materialID: [])
    
    
    
    
    
    
    var cdExample: [CleaningRota] = [bathroomMonday, kitchenFriday]  //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 ?

Terms of Service

Privacy Policy

Cookie Policy