We will now move on to delete a record from the persistent store. In our table view, we will load the customers using an instance of NSFetchRequest
, as shown:
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //Get the context first NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; //load data from Customer entity NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Customer"]; self.customers = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; [tblView reloadData]; }
Here, we will declare customers
as a mutable array to store our records from the Customer
entity:
@property (strong) NSMutableArray *customers;
To delete a record, we just need to get our Customer
record, which is an instance of NSManagedObject
from the customers
array, then use an instance of managedObjectContext
to call the deleteObject
method on...