Skip to content

Native to native data access (Draft)

wkh237 edited this page Jul 21, 2016 · 9 revisions

This document contains not implemented functions that will be available in the future.

Buffer

A Buffer is a byte array that stores in the native context. They can be write or append to a file, or used as body of fetch request. This feature greatly improve performance in some use cases.

For example, if you're concatenating two files, you may doing this

const fs = RNFetchBlob.fs
fs.readFile(path_of_file_1)
  .then((data) => fs.appendFile(path_of_file_2, data))
  .then(() => { console.log('DONE!') })
})

But in fact, the data of file1 read into native context, then converted to a JS context value, finally they're passed to JS context, and they're converted to again before we write them into a file, this is inefficient and may becomes performance impact to your app when data is large.

What if we use Buffer ?

const buffers = buffers
const fs = RNFetchBlob.fs

// create a buffer that reference to a file URI
buffers('ref_of_file_1').content(RNFetchBlob.wrap(path_of_file_1))

// append content of the buffer to file2, the whole process is done in native
fs.appendFile(path_of_file_2, buffers('ref_of_file_1'))
  .then(() => { console.log('DONE') })

The data will be read and write completely inside native context, that's native-to-native performance, without bridging data to JS context.

// TODO benchmark

API

RNFetchBlob.buffers(key:string):RNFetchBlobBuffer

Get a buffer entry by given key, if the entry not exists it will initialize the entry automatically.

key:string

Key of the buffer entry

TODO

Clone this wiki locally