Note that there are some explanatory texts on larger screens.

plurals
  1. POeffective way perform custom function inside a delegate method on objC
    primarykey
    data
    text
    <p>Just a question on how delegates work. </p> <p><strong>EDIT:</strong></p> <p>Because I may have confused you, here is a structure of my app.</p> <p><strong>LocationManager</strong> with some delegate functions.</p> <p>This class defines some delegate methods like:</p> <pre><code>@protocol LocationManagerDelegate &lt;NSObject&gt; @optional - (void)locationManager:(LocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance; @end </code></pre> <p>My <strong>MainViewController</strong> instantiates the LocationManager and implements the functions of the delegate.</p> <pre><code>[LocationManager sharedLocationManager].delegate = self; </code></pre> <p>So, inside the LocationManager there is the function:</p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation </code></pre> <p>and inside that I am calling a custom function f1, and a delegate function like this:</p> <pre><code> [self.delegate locationManager:self distanceUpdated:self.totalDistance]; </code></pre> <p>The code in the delegate function, implemented in my MainViewController is this:</p> <pre><code>- (void)locationManager:(LocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance { self.totalDistanceCovered.text= [NSString stringWithFormat:@"%.2f %@", distance, NSLocalizedString(@"meters", @"")]; } </code></pre> <p>So my question is:</p> <p>Which of this is more efficient and will not may my app blocks?</p> <p><em>This solution:</em></p> <p>in <strong>LocationManager</strong></p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { ... [self.delegate locationManager:self distanceUpdated:self.totalDistance]; ... f1(); } </code></pre> <p>in <strong>mainViewController</strong></p> <pre><code>- (void)locationManager:(PSLocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance { self.totalDistanceCovered.text= [NSString stringWithFormat:@"%.2f %@", distance, NSLocalizedString(@"meters", @"")]; } </code></pre> <p><strong>OR</strong></p> <p>in <strong>LocationManager</strong></p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { ... [self.delegate locationManager:self distanceUpdated:self.totalDistance]; ... } </code></pre> <p>in <strong>mainViewController</strong></p> <pre><code>- (void)locationManager:(PSLocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance { self.totalDistanceCovered.text= [NSString stringWithFormat:@"%.2f %@", distance, NSLocalizedString(@"meters", @"")]; f1(); } </code></pre> <p>Or are they completely identical?</p> <p>By that I mean that it will be implemented as a delegate method and the actual code will be in my mainViewController. (move the logic outside my didUpdate - and have it in my mainViewController)is it better? Because right now inside my didUpdate I am performing some extra things. Or is it the same?</p> <p>When a delegate method is called, does the place from where is it called stall and wait for the completion or it continues and runs independently from the delegate method? (I am having in my mind that it may be assigned to a different thread for example, so it does not stall - and therefore my did update will not wait for my custom function to finish but it will continue getting updates for locations).</p> <p>Can you help me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload