简体   繁体   中英

NSTimer - working on iOS 10 but not on iOS 9.3

I am using NSTimer in my iOS app developed in Xamarin. App is working fine on iOS 10 (iPhone 5S) but crashing on my iPad with with iOS 9.3.5.
Why is this happening and how can I fix it?

Here's the code:

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

});

And here is the crash log detail:

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 -- this is the code line where I have code for NStimer .

You can directly use System.Threading.Timer if you are in iOS project. If you want to use the timer in a PCL project, then you can refer to this link: https://forums.xamarin.com/discussion/17227/timer-in-portable-class-library

@Rob is right. You should use another methods if you're targeting iOS 9 or below.

This code translates to the newly block-syntax implementation which is not available on iOS versions prior to iOS 10:

[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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM