So I am starting to learn Core Data for iOS and have wrote this section of code out:
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
id delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *objectContext = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Album"];
//An array of our sorted Album objects by date in ascending order
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
NSError *error = nil;
NSArray *fetchedAlbums = [objectContext executeFetchRequest:fetchRequest error:&error];
self.albums = [fetchedAlbums mutableCopy];
[self.tableView reloadData];
}
My question is what is the purpose of this line:
id delegate = [UIApplication sharedApplication].delegate;
I know for a fact that for a delegate you need to pass in the current instance of a specific object to the delegate property which sets that object as the delegate for the other object. My question here is when you do:
id delegate = [UIApplication sharedApplication].delegate;
which object is being passed in to that delegate
property? The sharedApplication
method returns a singleton instance of the app I am assuming and then you are getting the delegate
property for that single instance of the app. Is that delegate referring to the same one being used in the AppDelegate.h
/AppDelegate.m
files?
And if that delegate property is the same one that is being used in AppDelegate.h
/AppDelegate.m
files shouldn't there be a line of code in the AppDelegate.m
file in the viewDidLoad
method where we be pass in the current instance of the AppDelegate.m
in the delegate property, similar to what we do for a tableView
:
self.tableView.delegate = self.
I don't see where that is done for the delegate
property in this line:
id delegate = [UIApplication sharedApplication].delegate;