jjzjj

c# - iOS 跟踪 CLCircularRegion - Heisenbug

coder 2024-01-18 原文

我的一个 iOS 应用程序似乎具有典型 Heisenbug 的症状。该应用程序跟踪用户的家庭位置,因此当用户进入和离开他们的家庭位置时会发生某些事件。

在我测试该应用程序时,它运行良好。我在 CLCircularRegion 中进进出出,无论我尝试哪种方式,它都能正常工作。它在后台与应用程序一起工作。它适用于关闭的应用程序。它与前台的应用程序一起工作。它适用于绿色鸡蛋和火腿。

不幸的是,用户报告会延迟 15 分钟左右的问题。用户将进入他们的家,但事件要等到以后才会发生。在某些情况下,事件根本不会发生。该模式似乎是当用户第一次开始使用该应用程序时,它运行良好。大约一天后,该应用程序似乎无法正常运行。事件延迟。

我会首先承认我不是 CLLocationManagerCLCircularRegion 内部工作的专家。我相信我已经正确设置了一切,但我真的很难弄清楚如何调试这样的东西。

无论如何,我会在这里展示我的一些代码。请记住,这是使用 Xamarin 开发的,因此它在 C# 中。

AppDelegate.cs

public static AppDelegate self;
private CLLocationManager locationManager;
private CLCircularRegion[] locationFences;



private void initializeLocationManager()
{
    this.locationManager = new CLLocationManager();

    // iOS 8 additional permissions requirements
    if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
    {
        locationManager.RequestAlwaysAuthorization();
    }

    locationManager.AuthorizationChanged += (sender, e) =>
    {
        var status = e.Status;

        // Location services was turned off or turned off for this specific application.
        if (status == CLAuthorizationStatus.Denied)
        {
            stopLocationUpdates();
        }
        else if (status == CLAuthorizationStatus.AuthorizedAlways &&
            iOSMethods.getKeyChainBool(OptionsViewController.GENERIC, OptionsViewController.SERVICE_GEOLOCATION_ENABLED))
        {
            startLocationUpdates();
        }
    };

    if (CLLocationManager.IsMonitoringAvailable(typeof(CLCircularRegion)))
    {
        locationManager.RegionEntered += (sender, e) =>
        {
            setRegionStatus(e, "Inside");
        };

        locationManager.RegionLeft += (sender, e) =>
        {
            setRegionStatus(e, "Outside");
        };

        locationManager.DidDetermineState += (sender, e) =>
        {
            setRegionStatus(e);
        };
    }
    else
    {
        // cant do it with this device
    }

    init();
}

public void init()
{
    var data = SQL.query<SQLTables.RoomLocationData>("SELECT * FROM RoomLocationData").ToArray();
    int dLen = data.Length;
    if (dLen > 0)
    {
        locationFences = new CLCircularRegion[dLen];
        for (int x = 0; x < dLen; x++)
        {
            var d = data[x];
            CLCircularRegion locationFence = new CLCircularRegion(new CLLocationCoordinate2D(d.Latitude, d.Longitude), d.Radius, d.SomeID.ToString() + ":" + d.AnotherID.ToString());
            locationFence.NotifyOnEntry = true;
            locationFence.NotifyOnExit = true;
            locationFences[x] = locationFence;
        }
    }
}

private void setRegionStatus(CLRegionEventArgs e, string status, bool calledFromDidDetermineState = false)
{
    string identifier = e.Region.Identifier;

    string lastStatus = iOSMethods.getKeyChainItem(OptionsViewController.GENERIC, OptionsViewController.SERVICE_LAST_GEO_STATUS);
    if (lastStatus == status + ":" + identifier)
    {
        return;
    }
    iOSMethods.setKeychainItem(OptionsViewController.GENERIC, OptionsViewController.SERVICE_LAST_GEO_STATUS, status + ":" + identifier);

    string[] split = identifier.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
    if (split.Length == 2)
    {
        try
        {
            int someID = Convert.ToInt32(split[0]);
            int anotherID = Convert.ToInt32(split[1]);

            // Notifies our API of a change.
            updateGeofenceStatus(someID, anotherID, status);

            if (iOSMethods.getKeyChainBool(OptionsViewController.GENERIC, OptionsViewController.SERVICE_GEOLOCATION_NOTIFICATIONS) &&
                (status == "Inside" || status == "Outside" || status == "Unknown"))
            {
                var rm = SQL.query<SQLTables.KeyRoomPropertyData>("SELECT * FROM KeyRoomPropertyData WHERE SomeID ID = ? AND AnotherID = ?",
                    new object[] { someID, anotherID }).ToArray();
                if (rm.Length > 0)
                {
                    if (status == "Unknown")
                    {
                        return;
                    }
                    var rmD = rm[0];
                    UILocalNotification notification = new UILocalNotification();
                    notification.AlertAction = "Geolocation Event";
                    notification.AlertBody = status == "Inside" ? "Entered " + rmD.SomeName + ": " + rmD.AnotherName :
                        status == "Outside" ? "Exited " + rmD.SomeName + ": " + rmD.AnotherName :
                        "Geolocation update failed. If you would like to continue to use Geolocation, please make sure location services are enabled and are allowed for this application.";
                    notification.SoundName = UILocalNotification.DefaultSoundName;
                    notification.FireDate = NSDate.Now;
                    UIApplication.SharedApplication.ScheduleLocalNotification(notification);
                }
            }
        }
        catch (Exception er)
        {
            // conversion failed. we don't have ints for some reason.
        }
    }
}

private void setRegionStatus(CLRegionStateDeterminedEventArgs e)
{
    string state = "";
    if (e.State == CLRegionState.Inside)
    {
        state = "Inside";
    }
    else if (e.State == CLRegionState.Outside)
    {
        state = "Outside";
    }
    else
    {
        state = "Unknown";
    }
    CLRegionEventArgs ee = new CLRegionEventArgs(e.Region);
    setRegionStatus(ee, state, true);
}

public void startLocationUpdates()
{
    if (CLLocationManager.LocationServicesEnabled)
    {
        init();
        if (locationFences != null)
        {
            foreach (CLCircularRegion location in locationFences)
            {
                locationManager.StartMonitoring(location);
                Timer t = new Timer(new TimerCallback(delegate(object o) { locationManager.RequestState(location); }), null, TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(-1));
            }
        }
    }
}

public void stopLocationUpdates(bool isRestarting = false)
{
    if (locationFences != null)
    {
        foreach (CLCircularRegion location in locationFences)
        {
            locationManager.StopMonitoring(location);
        }
    }
    if (!isRestarting)
    {
        var rooms = SQL.query<SQLTables.KeyRoomPropertyData>("SELECT * FROM KeyRoomPropertyData").ToArray();
        foreach (SQLTables.KeyRoomPropertyData room in rooms)
        {
            // notifies our API of a change
            updateGeofenceStatus(room.SomeID, room.AnotherID, "Unknown");
        }
    }
}

我知道有很多代码可供任何人筛选,但目前我真的没有很好的理论来说明是什么导致了这个错误,或者是否有可能修复 iOS 的限制。

我的一些理论是,如果 CLLocationManager.PausesLocationUpdatesAutomatically 属性可能与它有关,或者 CLLocationManager 的一些其他属性> 例如 ActivityTypeDesiredAccuracyDistanceFilter。我将所有这些都保留为默认值,我认为这没问题,但我不太确定。

另一种说法是,“服务”在后台运行了一段时间后,有一个未捕获的异常被抛出。如果是这样的话,iOS 有什么可以给我堆栈跟踪或其他东西的吗?在我所有的测试中,我从未遇到过从这段代码中抛出的任何异常,所以我有点怀疑这就是问题所在。不过在这一点上,我愿意接受任何想法或建议。

另外,请记住,为了让这个应用程序按预期方式工作,位置更新事件必须在用户进入或离开 CLCircularRegion 时立即发生(在一分钟内至少如此)。显然,我必须让用户保持启用他们的位置服务并允许应用程序拥有适当的权限。

最佳答案

您的诊断很可能是正确的 - 这是典型的观察者效应。

当您测试应用程序时,当用户玩新应用程序时,iphone 正在被积极使用。它没有机会休眠。第二天,当用户返回家时会发生什么——他们的手机很可能在到达家之前的很长一段时间内没有被使用:通常我们在离开公共(public)交通工具后的“最后一英里”步行过程中或开车回家时不使用手机家。 iOS 会注意到这种延长的不活动时间并调整自己的行为以优化电池生命周期。

观察这一点的最简单方法是组合一个简单的面包屑应用程序 - 在您的位置设置地理围栏,并在每次收到退出事件时继续这样做。根据您使用(或不使用)的方式,走同一条路线时您的手机结果会大不相同。

当您回到家时,手机通常也是您最不可能拿到的东西。

您可能希望用户提供更多详细信息,包括他们在回家前后 15 分钟内使用手机的具体情况、他们使用的其他应用程序、如果他们开车,他们是否会继续运行导航应用程序等。您将找出规律。

re. Also, please keep in mind that in order for this application to work the way it was intended, the location update events MUST occur as soon as the user enters or exist the CLCircularRegion (within a minute or so at least).

您不能只使用地理围栏来做到这一点,尤其是考虑到不同的到达/离开模式 - 步行与驾驶、“下降”路径(例如掉头到达)。您必须预料到超过 1 分钟的延迟和“过早”触发。恐怕没有解决方法。

关于c# - iOS 跟踪 CLCircularRegion - Heisenbug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35134729/

有关c# - iOS 跟踪 CLCircularRegion - Heisenbug的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  5. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  8. c# - C# 中的 Flatten Ruby 方法 - 2

    我如何做Ruby方法"Flatten"RubyMethod在C#中。此方法将锯齿状数组展平为一维数组。例如:s=[1,2,3]#=>[1,2,3]t=[4,5,6,[7,8]]#=>[4,5,6,[7,8]]a=[s,t,9,10]#=>[[1,2,3],[4,5,6,[7,8]],9,10]a.flatten#=>[1,2,3,4,5,6,7,8,9,10 最佳答案 递归解决方案:IEnumerableFlatten(IEnumerablearray){foreach(variteminarray){if(itemisIEnume

  9. ruby - 可以像在 C# 中使用#region 一样在 Ruby 中使用 begin/end 吗? - 2

    我最近从C#转向了Ruby,我发现自己无法制作可折叠的标记代码区域。我只是想到做这种事情应该没问题:classExamplebegin#agroupofmethodsdefmethod1..enddefmethod2..endenddefmethod3..endend...但是这样做真的可以吗?method1和method2最终与method3是同一种东西吗?还是有一些我还没有见过的用于执行此操作的Ruby惯用语? 最佳答案 正如其他人所说,这不会改变方法定义。但是,如果要标记方法组,为什么不使用Ruby语义来标记它们呢?您可以使用

  10. c# - Ruby 等效于 C# Linq 聚合方法 - 2

    什么是Linq聚合方法的ruby​​等价物。它的工作原理是这样的varfactorial=new[]{1,2,3,4,5}.Aggregate((acc,i)=>acc*i);每次将数组序列中的值传递给lambda时,变量acc都会累积。 最佳答案 这在数学以及几乎所有编程语言中通常称为折叠。它是更普遍的变形概念的一个实例。Ruby从Smalltalk中继承了这个特性的名称,它被称为inject:into:(像aCollectioninject:aStartValueinto:aBlock一样使用。)所以,在Ruby中,它称为inj

随机推荐