0

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

编辑:

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

具有一些委托功能的LocationManager 。

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

@protocol LocationManagerDelegate <NSObject>

@optional
- (void)locationManager:(LocationManager *)locationManager distanceUpdated:(CLLocationDistance)distance;

@end

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

[LocationManager sharedLocationManager].delegate = self;

所以,在 LocationManager 里面有这个函数:

- (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();
}

还是它们完全一样?

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

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

你能帮助我吗?

4

2 回答 2

1

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

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

避免性能问题的方法是让您的委托成为“模型”对象 - 而不是调用 UI 工具包的东西。你应该有一个单独的 NSNotification 监听器,比如 updateUIFromModel,每当你的模型更新到 UI 需要更新的程度时,它就会发出信号。这个监听器应该从主线程调度,所以它只更新主线程上的 UI 相关项。当您的位置相关委托被调用时,您可以发出通知让您的侦听器获取并更新 UI。

于 2013-09-13T20:51:42.357 回答
1

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

话虽如此,任何类型的 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];
        }
    });
}
于 2013-09-13T20:47:53.113 回答