iphone可可nsrunloop的使用

发布于 2024-08-06 20:47:17 字数 984 浏览 5 评论 0原文

我发现自己使用全局变量和 nsrunloop 的组合来强制整个应用程序同步。虽然它有效,但对我来说似乎有点难看。还有其他方法可以达到相同的结果吗?

这是一个典型的示例:

ParkingSpots *parkingSpots = [[[ParkingSpots alloc] initWithMapViewController:self] autorelease];
        keepRunning = YES;
        NSRunLoop *theRL = [NSRunLoop currentRunLoop];
        while (keepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

        UpdateLocation *updatedLocation = [[[UpdateLocation alloc] initWithUserid:@"me" andCoordinate:annotation.coordinate withMapViewController:self]
                                           autorelease];
        NSLog(@"Lat = %f, Long = %f",annotation.coordinate.latitude,annotation.coordinate.longitude);
        [updatedLocation sendUpdate];

在这段代码中,我需要等到 parkingSpots 对象完全初始化后再初始化 updateLocation。由于 updatelocation 期望 parkingSpots 完全初始化,因此如果没有运行循环,updatedlocation 就无法正确初始化。通过运行循环,一切都按预期工作。

然而,这对我来说看起来非常丑陋(在我的代码中的各个点设置全局变量)。有更优雅的解决方案吗?预先感谢您的帮助!

I find myself using a combination of global vars and nsrunloop to force synchronization throughout my application. Although it works it seems a bit ugly to me. Is there any other way of achieving the same result?

Here's a typically example:

ParkingSpots *parkingSpots = [[[ParkingSpots alloc] initWithMapViewController:self] autorelease];
        keepRunning = YES;
        NSRunLoop *theRL = [NSRunLoop currentRunLoop];
        while (keepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

        UpdateLocation *updatedLocation = [[[UpdateLocation alloc] initWithUserid:@"me" andCoordinate:annotation.coordinate withMapViewController:self]
                                           autorelease];
        NSLog(@"Lat = %f, Long = %f",annotation.coordinate.latitude,annotation.coordinate.longitude);
        [updatedLocation sendUpdate];

In this code I need to wait until the parkingSpots object is completely initialized before I initialize updateLocation. Since updatelocation expects parkingSpots to be fully initialized, without the runloop updatedlocation was not initializing properly. WIth the runloop everything works as expected.

However this looks very ugly to me (setting a global var at various points in my code). Is there a more elegant solution? Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2024-08-13 20:47:17

您可以在 ParkingSpots 类上使用委托模式,并在完成初始化时调用该委托。例如,

ParkingSpots *parkingSpots = [[[ParkingSpots alloc] initWithMapViewController:self] autorelease];
parkingSpots.delegate = self;
parkingSpots.didInitialiseSelector = @selector(parkingSpotsInitialised:);
[parkingSpots startLengthyInitialisation];

- (void) parkingSpotsInitialised:(ParkingSpots*)parkingSpots {
  UpdateLocation *updatedLocation = [[[UpdateLocation alloc] initWithUserid:@"me" andCoordinate:annotation.coordinate withMapViewController:self] autorelease];
}

您还可以使用通知来实现相同的目的。

You could use the delegate pattern on your ParkingSpots class, and call the delegate when it finishes initialising. e.g.

ParkingSpots *parkingSpots = [[[ParkingSpots alloc] initWithMapViewController:self] autorelease];
parkingSpots.delegate = self;
parkingSpots.didInitialiseSelector = @selector(parkingSpotsInitialised:);
[parkingSpots startLengthyInitialisation];

- (void) parkingSpotsInitialised:(ParkingSpots*)parkingSpots {
  UpdateLocation *updatedLocation = [[[UpdateLocation alloc] initWithUserid:@"me" andCoordinate:annotation.coordinate withMapViewController:self] autorelease];
}

You could also use notifications to achieve the same thing.

等风来 2024-08-13 20:47:17

我认为你需要看看 Objective-C 的 同步功能

I think you need to look at objective-c's synchronization feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文