菜鸟教程小白 发表于 2022-12-12 12:17:09

iOS 阻止日期/时间更新?


                                            <p><p>一个相对简单的问题,我无法找到明确的答案。我的应用比较复杂,但回答这个问题就足够了。</p>

<p>假设您正在编写一个秒表应用程序。当用户点击“开始”时,应用会将当前日期和时间存储在 startTime 中:</p>

<pre><code>startTime = ;
</code></pre>

<p>当用户录制“stop”时,应用程序将当前日期和时间存储在 stopTime 中:</p>

<pre><code>stopTime = ;
</code></pre>

<p>持续时间通过以下方式计算:</p>

<pre><code>duration = ;
</code></pre>

<p>并显示如下:</p>

<pre><code>];
</code></pre>

<p>我的应用程序计时的典型持续时间范围为 2 到 50 秒。我需要精确到 1/100 秒(例如 2.86 秒)。</p>

<p>我假设 iOS 设备使用某些协议(protocol)来保持其时钟准确(蜂窝或 NTP 源?)。我担心的是,在启动和停止秒表之间,iOS 设备上的时钟会更新,这可能会导致当前日期/时间向前或向后移动。如果发生这种情况,计算的持续时间将不准确。</p>

<p>我看过一些与计时方法相关的帖子,目的是提高代码效率。有些人建议使用我不熟悉的 mach_time.h 函数。对我来说,使用哪种方法最好并不明显。</p>

<p>是否可以禁止 iOS 更新日期和时间? mach_absolute_time() 不受 iOS 时钟更新的影响吗?</p>

<p>非常感谢!</p>

<p>提姆</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您认为 <code>CFAbsoluteTime</code> 及其派生类(<code>NSDate date</code> 等)可能会受到“实时”网络更新的影响是正确的。再加上 NSTimer 的精度为 50-100 毫秒,而您的计时器不适合最关键的时间敏感操作。</p>

<p>这个问题的答案似乎是<code>CACurrentMediaTime</code>。
它是 Core Animation 组的成员,但将它集成到非基于动画的应用程序中应该没有任何问题。 </p>

<p><code>CACurrentMediaTime</code> 是 mach_absolute_time() 的包装器,并且可以理解“马赫绝对时间单位”,据我所知,修改它并不有趣。 mach_absolute_time() 是通过自设备上次启动后运行非网络同步计时器来计算的。 </p>

<p>关于 <code>CACurrentMediaTime</code> 的信息相对较少,但这里有一些来源和进一步阅读:</p>

<ul>
<li> <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreAnimation_functions/Reference/reference.html#//apple_ref/c/func/CACurrentMediaTime" rel="noreferrer noopener nofollow">Apple&#39;s sparse documentation of CACurrentMediaTime</a> </li>
<li>堆栈溢出 - <a href="https://stackoverflow.com/questions/11660403/nstimer-vs-cacurrentmediatime" rel="noreferrer noopener nofollow">NSTimer vs CACurrentMediaTime()</a> </li>
<li> <a href="http://bendodsonapps.com/weblog/2013/01/29/ca-current-media-time/" rel="noreferrer noopener nofollow">http://bendodsonapps.com/weblog/2013/01/29/ca-current-media-time/</a> </li>
<li> <a href="http://blog.spacemanlabs.com/2011/09/all-in-the-timing-keeping-track-of-time-passed-on-ios/" rel="noreferrer noopener nofollow">http://blog.spacemanlabs.com/2011/09/all-in-the-timing-keeping-track-of-time-passed-on-ios/</a> </li>
<li> <a href="http://forum.sparrow-framework.org/topic/accurate-timer" rel="noreferrer noopener nofollow">http://forum.sparrow-framework.org/topic/accurate-timer</a> </li>
</ul>

<p>注意:如果您使用 <code>CACurrentMediaTime</code>,请确保包含并链接 QuartzCore.framework</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 阻止日期/时间更新?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17496655/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17496655/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 阻止日期/时间更新?