react-native-background-upload icon indicating copy to clipboard operation
react-native-background-upload copied to clipboard

How remove the listener events?

Open luigbren opened this issue 5 years ago • 4 comments

How can I delete the EventListener (Progress, Complete) when they finish their work? Because, I upload a file, when it is completed in the notifications it shows the message "completed" and when I upload another file, it doesn't do anything, it doesn't upload the file, I have to close the application and come back

Returns an EventSubscription. To remove the listener, call remove() on the EventSubscription. ????

.then((uploadId) => {
                Upload.addListener('progress', uploadId, (data) => {
                    console.log(`Progress: ${data.progress}%`)
                    if (data.progress == 100) {
                        //Remove EventListener
                    }
                })
                Upload.addListener('error', uploadId, (data) => {
                    console.log(`Error: ${data.error}%`)
                })
                Upload.addListener('cancelled', uploadId, (data) => {
                    console.log(`Cancelled!`)
                })
                Upload.addListener('completed', uploadId, (data) => {
                    // data includes responseCode: number and responseBody: Object
                    console.log('Completed!')
                    //Remove EventListener
                })

luigbren avatar Jun 20 '20 02:06 luigbren

@luigbren To remove a listener, you would do something like const listener = Upload.addListener(....) and then listener.remove(). I saw that the TypeScript definitions may not be correct here...

reime005 avatar Jun 21 '20 10:06 reime005

Overwrite the package was the only solution for remove events. Ex : add this line in packages index.js file export const removeListener = () => { return DeviceEventEmitter.removeAllListeners(); }; export default { startUpload, cancelUpload, addListener, getFileInfo, removeListener }; and add these line in index.d.ts file static removeListener(): void;

Upload.removeListener() call this function in inside service it after complete response or before service call (based upon your requirement)

selvaprogrammer avatar Jul 09 '20 11:07 selvaprogrammer

Another solution i have found : import DeviceEventEmitter in current scene. Upload.addListener('progress', uploadId, (data) => { console.log(Progress: ${data.progress}%); if (data.progress === 100) DeviceEventEmitter.removeCurrentListener(); }); Upload.addListener('error', uploadId, (data) => { console.log(Error: ${data.error}%); DeviceEventEmitter.removeCurrentListener(); }); Upload.addListener('completed', uploadId, (data) => { let some = JSON.parse(data.responseBody); DeviceEventEmitter.removeCurrentListener(); });

removeCurrentListener removes that listener . Try it.

selvaprogrammer avatar Jul 09 '20 12:07 selvaprogrammer

@selvaprogrammer DeviceEventEmitter is deprecated now

meidikawardana avatar Apr 07 '22 03:04 meidikawardana