objective-c - 减少内存大小以确保 iOS 中的后台运行

标签 objective-c ios background-process core-bluetooth bluetooth-lowenergy

在开发使用低功耗蓝牙的应用程序时,iOS 设备有时会失去与外围设备的连接。 (有时几个小时。)

为了重新连接到现有外围设备,应用程序必须全天以特定速率在后台不断扫描,即使应用程序处于后台也是如此。

问题是,由于内存限制等原因,iOS 无法保证您的应用不会被杀死。

Information found in the iPhone OS Programming guide指出:

Apps that work with Bluetooth peripherals can ask to be woken up if the peripheral delivers an update when the app is suspended. This support is important for Bluetooth-le accessories that deliver data at regular intervals, such as a Bluetooth heart rate belt. When an app includes the UIBackgroundModes key with the bluetooth-central value in its Info.plist file, the Core Bluetooth framework keeps open any active sessions for the corresponding peripheral. In addition, new data arriving from the peripheral causes the system to wake up the app so that it can process the data. The system also wakes up the app to process accessory connection and disconnection notifications.

当手机连接到设备且应用程序处于后台时,不会出现此问题。但是,当设备断开连接并且应用程序处于后台时,它确实会发生。在这种特定情况下,手机不再连接到外围设备,因此不再收到通知。

许多人之前在 Stack Overflow 或 Apple 论坛上讨论过这个问题,我相信其中一位 Apple 开发人员已经回应说:

We're aware of this issue and are trying to come up with a solution. Currently, there is no workaround."

我的问题是,有没有办法至少提高你不会因为内存限制而被 iOS 杀死的几率?

例如,即时通讯应用程序 (IMO) 似乎在后台运行得非常好。在几天没有被使用后,该应用程序将唤醒并显示一条 gChat 消息。

我在质疑诸如

  • 强指针
  • 总内存大小
  • 在应用后台运行或最小化时减少内存大小
  • 减少后台操作的频率

最佳答案

为什么即使蓝牙硬件断开连接也需要后台执行? 我不认为你需要“连续重新扫描”才能再次重新连接,如果硬件与 iPhone/iPad“配对”,它会自行重新连接。比如蓝牙耳机。还是不行?

据我所知,您没有机会完成您的要求。 当用户回到家时,一个普通的应用程序总是被挂起。该应用程序有大约。 5 秒的背景时间停止计时器,保存状态 ecc ecc。 有一些特殊的后台模式可以让您有更多的后台时间,并且每种模式(在您链接的页面中解释)都有不同的行为。

关于蓝牙模式: 描述的行为不是问题,但它是设计使然:

  • 应用已暂停

  • 当应用挂起时,它可以被操作系统杀死以释放内存(并且没有避免这种情况的技巧),但系统会在需要时唤醒。

  • 然后每次收到通知时都会唤醒应用(从挂起状态唤醒或从“先前终止”状态启动)

  • 应用程序有 10 秒的时间来执行任务(保存信息、ecc ecc)。此外,可以要求+10 分钟。特定任务的后台时间

  • 10 秒(或 10 分钟)后应用再次挂起

您写的关于聊天应用程序的示例不正确:聊天应用程序通常不使用任何后台模式,它们只是使用推送通知向您转发消息。当您打开该应用程序时,该应用程序会连接到存储您所有消息的服务器并下载它。

您可以使用位置后台模式(路由应用程序可以在后台运行)或结合使用显着位置变化(应用程序被唤醒)和 10 分钟后台时间来获得“更多正常运行时间”,但我认为 Apple 会拒绝“滥用”此功能的应用。

很快,您必须设计您的应用以支持此行为。

关于objective-c - 减少内存大小以确保 iOS 中的后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200123/

相关文章:

iphone - iOS 媒体框架

ios - RxBluetoothKit : How to subscribe to Bluetooth state + Peripheral connection state and Write/Notify characteristics at same time?

ios - 哪个是在 Swift 中显示不规则形状的更好选择?

c# - 从另一个任务访问 BackgroundMediaPlayer

ruby-on-rails - sidekiq 后台作业发送电子邮件和 heroku worker

ASP.NET 后台处理

ios - 如何为 UITableView 部分页脚设置动画

iphone - 与 Three20 集成的 RestKit

objective-c - 跟随手指旋转的炮塔/大炮的 iOS 数学

ios - Appirater 警报未显示