0

I am new to ios. I am trying to fetch a specific data from my core data. I have already created the entity and it's attribute. I have a coursework entity and several attributes inside it as well as a task entity. The relationship between coursework and tasks is a 1-to-many.

I am have implemented my saving function to save courseworks to core data. I am now trying to implement a save function to save tasks to a coursework object (depending on what the user selected etc). I am new to this concept so the way I am thinking of coding it is to get the coursework object, create the task object, and place the task within the coursework object essentially UPDATING the coursework object? Is this the correct way on going about this situation?

So right now, I would like to fetch a specific coursework based on a few attributes but my predicate doesn't seem to be working and I'm not sure why. This is what I have in my save task function (it's not fully implemented):

@IBAction func saveTask(_ sender: UIButton) {
    //First validate the form

    //save if validation successful
    if(validateTextField(tf: textFieldTaskName) && validateDate(startDate: startingDate, dueDate: dueDate)){
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext

        let fetchrequest = NSFetchRequest<NSManagedObject>(entityName: "Coursework")
        print(courseworkLevel + courseworkName)
        fetchrequest.predicate = NSPredicate(format: "%K == %@ AND %K == %@", "courseworkName", courseworkName, "level", Int16(courseworkLevel)!)

        do{
            let profiles = try context.fetch(fetchrequest) as! Coursework

            print("SUCCESSFULLY FETCHED" + profiles.courseworkName!);
        }catch{
            print("...UNABLE TO FETCH")
        }
    }}

The error comes on the fetchrequest.predicate = NSPredicate(format: "%K == %@ AND %K == %@", "courseworkName", courseworkName, "level", Int16(courseworkLevel)!) line and the print statement just before this code does confirm those variables I am using to filter are populated with the right value. I am getting the following error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x4) and also

 2018-05-09 12:33:16.668196+0100 dummyapp[11954:376059] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/jack/Library/Developer/CoreSimulator/Devices/78BDDD68-8B2E-4C0A-80AD-27604D054E09/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-05-09 12:33:16.668563+0100 dummyapp[11954:376059] [MC] Reading from private effective user settings.

Could someone explain why this is occurring and also if my method in terms of creating and updating objects using core data is correct?

  • `%@` with an `Int` => Crash. That's why. See this: https://stackoverflow.com/questions/23610509/exc-bad-access-when-building-nspredicate/27566124#27566124 or https://stackoverflow.com/questions/42362669/exc-bad-access-crash-on-localized-string-with-parameter/42392523#42392523 It's related to the `stringWithFormat:` not specific to `NSPredicate`. – Larme May 09 '18 at 11:45
  • Cheers it works now! I was searching all night and couldn't find anything! – tloz1506996 May 09 '18 at 12:00

0 Answers0