Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate Property for UIApplication

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;
like image 697
Garry Harry Avatar asked Sep 27 '22 23:09

Garry Harry


1 Answers

You do not need to assign your AppDelegate as a delegate for your application because it is being automatically done for you.

In objective-c you can look at main.m function where you can find the mentioning of you AppDelegate class:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
        return retVal;
    }
}

After this call the system will set the instance of class AppDelegate as a delegate for your application.

In Swift, main.m is missing which is why you always have @UIApplicationMain before the declaration of your delegate class:

@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
}

Because UIApplication.sharedApplication() is a singleton, UIApplication.sharedApplication().delegate always returns the same instance of AppDelegate.

So, if you modify some property in your AppDelegate from one view controller you can be sure that the other view controllers will have access to the modified version. You can easily verify it yourself by implementing a simple counter and checking that you always can read the latest value :)

like image 125
Andriy Gordiychuk Avatar answered Sep 30 '22 06:09

Andriy Gordiychuk