pjivers
pjivers

Reputation: 1939

How to handle memory warnings in React Native iOS?

Background: I'm implementing a native iOS map component in Swift to be used in my React Native app. It lazy loads tiles onto the map as the user zooms / pans around, which causes the memory usage to gradually increase.

Problem: Occasionally, the memory usage gets too high and I need to clear the tile cache to bring it back down. In iOS, I understand that you can implement didReceiveMemoryWarning in the UIViewController to release some memory, but native iOS components for React Native don't have a UIViewController, only a UIView.

Tried: I've tried listening for memory warnings at the React Native level (suggested here), and then calling a native method exposed by RCT_EXTERN_METHOD to free up some memory. However, I'd rather the native component take care of itself in terms of cleaning up memory.

TL;DR: How do I handle memory warnings at the iOS level for a React Native "native iOS" component?

Upvotes: 0

Views: 1383

Answers (1)

Asperi
Asperi

Reputation: 257749

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

If you hold and manage tiles in native subclass of UIViewController (eg. TileViewController), then the simplest way is to do cleanup like in the following

class TileViewController: UIViewController {

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

        // clean up cached tiles here
    }
}

Upvotes: 4

Related Questions