【问题标题】:NSTimer - working on iOS 10 but not on iOS 9.3NSTimer - 适用于 iOS 10 但不适用于 iOS 9.3
【发布时间】:2016-12-24 06:32:55
【问题描述】:

我在用 Xamarin 开发的 iOS 应用程序中使用 NSTimer。应用程序在 iOS 10 (iPhone 5S) 上运行良好,但在装有 iOS 9.3.5 的 iPad 上崩溃。
为什么会发生这种情况,我该如何解决?

代码如下:

timer = NSTimer.CreateScheduledTimer(0.3, true, delegate
{
    List<String> keys = this.beaconRegions.Keys.ToList();
    foreach (string identifier in keys)
    {
        this.locationManager.StopRangingBeacons((CLBeaconRegion)this.beaconRegions[identifier]);
        this.locationManager.StopMonitoring(this.beaconRegions[identifier]);
        this.locationManager.StartMonitoring(this.beaconRegions[identifier]);
        this.locationManager.StartRangingBeacons(this.beaconRegions[identifier]);
    }

});

这是崩溃日志的详细信息:

4   CoreFoundation                  0x21c9a234 0x21c72000 + 164404

5   iBeacon                         0x0023564c wrapper_managed_to_native_ObjCRuntime_Messaging_objc_msgSend_intptr_intptr_double_bool_intptr (/<unknown>:1)

6   iBeacon                         0x001e8ecc Foundation_NSTimer_CreateScheduledTimer_double_bool_System_Action_1_Foundation_NSTimer (NSTimer.g.cs:134)

7   iBeacon                         0x001de5b4 iBeacon_LocationManager_turnOnMonitoring (LocationManager.cs:277)

8   iBeacon                         0x001de2ac iBeacon_LocationManager_startMonitoringForBeacons (LocationManager.cs:250)

LocationManager.cs:277 -- 这是我有 NStimer 代码的代码行。

【问题讨论】:

  • 我不是 xamarin 人,但我想知道这是否会转化为 iOS 10 中引入的基于块的 NSTimer API(请参阅 scheduledTimer(withTimeInterval:repeats:block:))。您必须使用如果您要支持早期的 iO​​S 版本,请使用旧的 target/selector 再现。
  • @Kyll 好的。

标签: c# ios iphone xamarin.ios nstimer


【解决方案1】:

如果您在 iOS 项目中,可以直接使用System.Threading.Timer。 如果你想在PCL项目中使用定时器,可以参考这个链接:https://forums.xamarin.com/discussion/17227/timer-in-portable-class-library

【讨论】:

    【解决方案2】:

    @Rob 是对的。如果您的目标是 iOS 9 或更低版本,您应该使用其他方法。

    此代码转换为新的块语法实现,该实现在 iOS 10 之前的 iOS 版本中不可用:

    [ObjCRuntime.Introduced(ObjCRuntime.PlatformName.iOS, 10, 0, ObjCRuntime.PlatformArchitecture.None, null)]

    https://developer.xamarin.com/api/member/Foundation.NSTimer.CreateScheduledTimer/p/System.Double/System.Boolean/System.Action%7BFoundation.NSTimer%7D/

    【讨论】:

    • 好的,有什么替代方法可以在以前的 iOS 版本上使用?
    • @darshana 我的 9.3 应用已轻松应对问题部分中给出的 Nstimer!
    猜你喜欢
    • 1970-01-01
    • 2014-04-06
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多