如何处理 React Native iOS 中的内存警告?

How to handle memory warnings in React Native iOS?

背景: 我正在 Swift 中实现原生 iOS 地图组件,以用于我的 React Native 应用程序。它会在用户缩放/平移时延迟将图块加载到地图上,这会导致内存使用量逐渐增加。

问题: 有时,内存使用率过高,我需要清除切片缓存以将其恢复。在 iOS 中,我了解到您可以在 UIViewController 中实现 didReceiveMemoryWarning 以释放一些内存,但是 React Native 的原生 iOS 组件没有 UIViewController, 只有一个 UIView.

试过: 我试过在 React Native 级别监听内存警告(建议 ),然后调用 [=15] 公开的本地方法=] 释放一些内存。但是,我宁愿本机组件在清理内存方面自行处理。

TL;DR: 如何处理 React Native "native iOS" 组件的 iOS 级别的内存警告?

However, I'd rather the native component take care of itself in terms of cleaning up memory.

如果您在 UIViewController(例如 TileViewController)的本机子类中持有和管理图块,那么最简单的方法就是像下面那样进行清理

class TileViewController: UIViewController {

    // this called automatically on system memory warning
    override func didReceiveMemoryWarning() { 
        super.didReceiveMemoryWarning()

        // clean up cached tiles here
    }
}