在 objC 上的委托方法内执行自定义函数的有效方法



只是一个关于代表如何工作的问题。

编辑:

因为我可能让你感到困惑,这里是我的应用程序的结构。

具有一些委托功能的位置管理器。

此类定义了一些委托方法,例如:

@protocol LocationManagerDelegate <NSObject>
@optional
- (void)locationManager:(LocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance;
@end

My MainViewController 实例化 LocationManager 并实现委托的功能。

[LocationManager sharedLocationManager].delegate = self;

因此,在位置管理器中有一个功能:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

在其中,我调用了一个自定义函数 f1,以及如下所示的委托函数:

        [self.delegate locationManager:self distanceUpdated:self.totalDistance];

在我的 MainViewController 中实现的委托函数中的代码是这样的:

- (void)locationManager:(LocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance {
    self.totalDistanceCovered.text= [NSString stringWithFormat:@"%.2f %@", distance, NSLocalizedString(@"meters", @"")];
}

所以我的问题是:

其中哪一个更有效,并且不会阻止我的应用程序?

此解决方案:

位置管理器

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    ...
                [self.delegate locationManager:self distanceUpdated:self.totalDistance];
    ...
    f1();
    }

主视图控制器

- (void)locationManager:(PSLocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance {
    self.totalDistanceCovered.text= [NSString stringWithFormat:@"%.2f %@", distance, NSLocalizedString(@"meters", @"")];
}

位置管理器

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
...
            [self.delegate locationManager:self distanceUpdated:self.totalDistance];
...
}

主视图控制器

- (void)locationManager:(PSLocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance {
    self.totalDistanceCovered.text= [NSString stringWithFormat:@"%.2f %@", distance, NSLocalizedString(@"meters", @"")];
f1();
}

还是它们完全相同?

我的意思是它将作为委托方法实现,实际代码将在我的主视图控制器中。(将逻辑移到我的 didUpdate 之外 - 并将其放在我的主视图控制器中)它更好吗?因为现在在我的 didUpdate 中,我正在执行一些额外的操作。还是一样?

调用委托方法时,调用委托方法的位置

是停滞并等待完成,还是继续独立于委托方法运行?(例如,我想到它可能被分配给不同的线程,因此它不会停止 - 因此我的 did 更新不会等待我的自定义函数完成,但它会继续获取位置的更新)。

你可以帮我吗?

当您提供委托方法时,运行需要调用委托的线程将被阻止,直到您的方法返回(假设它们没有专门实现其异步回调的调用)。

如果您的委托方法更新与 UI 相关的项(就像您的方法一样),则会遇到问题,因为必须在主线程上处理与 UI 相关的项。

避免性能问题的方法是让委托成为"模型"对象 - 而不是调用 UI 工具包的内容。 您应该有一个单独的 NSNotification 侦听器,例如 updateUIFromModel,每当您的模型更新到 UI 需要更新的程度时,都会发出信号。 此侦听器应从主线程计划,因此它仅更新主线程上与 UI 相关的项。 调用与位置相关的委托时,可以发出通知,让侦听器选取和更新 UI。

调用委托方法与调用任何其他方法没有什么不同。无论调用该方法的任何线程都是该方法将在其中运行的线程。

话虽如此,任何类型的 UI 更新(您提到了标签)都必须在主线程上进行,否则您将看到一些奇怪的结果。

编辑:

这是处理线程之间的委派时的常见模式。

- (void)main {
    // This is running some code on a background thread.
    dispatch_async(dispatch_get_main_queue(), ^{
        // This is the main thread. Notify the delegate here.
        if ([delegate respondsToSelector:@selector(finishedDoingBackgroundWork)]) {
            [delegate finishedDoingBackgroundWork];
        }
    });
}

最新更新