Skip to content

Native to native data access (Draft)

wkh237 edited this page Jul 31, 2016 · 9 revisions

Functions in this document are NOT yet implemented, you can't use them in any release

Cache

A Cache is a byte array that stores in the native context. They can be write, replace,and append to a file, or used become 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 Cache ?

const cache = RNFetchBlob.cache
const fs = RNFetchBlob.fs

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

// append content of the cache to file2, the whole process is done in native
fs.appendFile(path_of_file_2, cache('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.cache(key:string):RNFetchBlobBuffer

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

key:string

Key of the cache entry

TODO