dimanche 10 mai 2015

Adding to a NSCompoundPredicate

I want to make my code more reusable for core data, I have a fetchRequest with predicates that I always must use. However some methods require more conditions. I want to add those conditions to the predicate list however I am unsure how to do this. I would like a method to return a predicate with basic queries and then add on to those queries.

 let fetchRequest = NSFetchRequest(entityName: "Stop")

        var currentTime = NSDate.getTime()

        var sort = NSSortDescriptor(key: "time", ascending: true) // sort by bus stop

        fetchRequest.sortDescriptors = [sort]

        let predicate = NSPredicate(format: "time >= %ld", currentTime)

        let predicate2 = NSPredicate(format: "stop_name == %@", stop)

        let predicate3 = NSPredicate(format: "busParent.direction == %@", direction)

        let predicate4 = NSPredicate(format: "busParent.name == %@", name)

        let predicate5 = NSPredicate(format: "busParent.schedule == %ld", schedule)

        fetchRequest.predicate = NSCompoundPredicate.andPredicateWithSubpredicates(
            [predicate, predicate2, predicate3, predicate4, predicate5])

        // EXAMPLE:  HOW WOULD I ADD TO THE COMPOUND PREDICATE ALREADY MADE?
        fetchRequest.predicate.????

Aucun commentaire:

Enregistrer un commentaire