Skip to content

Instantly share code, notes, and snippets.

@jtremback
Last active August 29, 2015 14:25
Show Gist options
  • Save jtremback/8732dc515384250b8c60 to your computer and use it in GitHub Desktop.
Save jtremback/8732dc515384250b8c60 to your computer and use it in GitHub Desktop.
React Native pointers

In react native, we have a need to handle pieces of data that are too large to move over the objc-js bridge. Currently, I do this by putting the data on the file system, reading it from there, then erasing it when done. This is just like manual memory management, but slower, and any "memory leaks" will persist and continue to take up space until the app is deleted. It would be good to have pointers to objc memory accesible from JS.

To evaluate different ways of doing this, let's consider a hypothetical application. A camera module needs to take a photo which is then uploaded by an http module.

Singleton dictionary

This would involve the camera and http module both having compatibility with rn pointers, and the user installing a 3rd module, rn-pointers. The rn-pointers module would mostly consist of a singleton dictionary in objc.

  • When an rn-pointers compatible module wishes to return a pointer, it generates a UUID, and sends an event with that UUID and a payload of the data in question.
  • Rn-pointers recieves the event and creates a key in it's dictionary of the UUID storing the data.
  • JS receives a string of the UUID.
  • To retreive the data, an rn-pointers compatible module sends an event with the UUID requesting that data.
  • Rn-pointers receives this event and sends an event back with the UUID and payload.

There would also be a way to dealloc etc.

Data stored in module, no singleton

  • When an rn-pointers compatible module wishes to return a pointer, it generates a UUID, and stores the data in a dict under the key of that UUID.
  • It returns a string of the UUID to JS
  • To resolve the data, a module receiving the UUID string sends an event with the UUID and listens for a response
  • The first module sends an event with the data and deletes the data from it's dict.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment