Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Session API

wkh237 edited this page Sep 17, 2016 · 6 revisions

RNFetchblob.session

Class RNFetchBlobSession

0.5.0

A session is an object that helps you manage files. It simply maintains a list of file path and let you use dispose()to delete files in this session once and for all.

The session table is maintained in memory, which means you may lost the session records if the app is closed. If you need to persist session data, consider use AsyncStorage or react-native-storage to maintain your sessions.

add(path:string):RNFetchBlobSession

Add a file path to this session.

remove(path:string):RNFetchBlobSession

Remove a session entry from this session without delete the file.

list():Array

Returns an array contains file paths in this session.

dispose():Promise

Delete all files in the session.

Cache File Management

When using fileCache or path options along with fetch API, response data will automatically stored into file system. The files will NOT removed unless you unlink it. There're several ways to remove the files

  // remove file using RNFetchblobResponse.flush() object method
  RNFetchblob.config({
      fileCache : true
    })
    .fetch('GET', 'http://example.com/download/file')
    .then((res) => {
      // remove cached file from storage
      res.flush()
    })

  // remove file by specifying a path
  RNFetchBlob.fs.unlink('some-file-path').then(() => {
    // ...
  })

You can also grouping requests by using session API, and use dispose to remove them all when needed.

  RNFetchblob.config({
    fileCache : true
  })
  .fetch('GET', 'http://example.com/download/file')
  .then((res) => {
    // set session of a response
    res.session('foo')
  })  

  RNFetchblob.config({
    // you can also set session beforehand
    session : 'foo'
    fileCache : true
  })
  .fetch('GET', 'http://example.com/download/file')
  .then((res) => {
    // ...
  })  

  // or put an existing file path to the session
  RNFetchBlob.session('foo').add('some-file-path')
  // remove a file path from the session (just remove the entry, the file won't be deleted)
  RNFetchBlob.session('foo').remove('some-file-path')
  // list paths of a session
  RNFetchBlob.session('foo').list()
  // delete all files in a session
  RNFetchBlob.session('foo').dispose().then(() => { ... })