I store data using the following method
- (NSManagedObject *) createCourseWithCourseCode:(NSString *) courseCode {
NSManagedObject *course = [NSEntityDescription insertNewObjectForEntityForName:@"Course" inManagedObjectContext:context];
[course setValue:courseCode forKey:@"CourseCode"];
[self saveChanges];
return course;
}
And then I tried to delete with this code, where I am getting the NSManagedObject from a fetch method, but this is not working. ‘An NSManagedObjectContext cannot delete objects in other contexts.’
- (void) removeCourseWithCourseCode:(NSManagedObject *) courseCode {
[context deleteObject:courseCode];
[self saveChanges];
}
I was wondering if I could simply delete the object by finding where the key CourseCode matches a string?
You can do this by passing the object’s
objectIDinstead of the actual object.Your code would look like this:
And you can access an
NSManagedObject‘s ID like this:So your first block of code should look something like this: