Browse Source

Fix session dispose bug on android

Ben Hsieh 8 years ago
parent
commit
5413016409

+ 10
- 5
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -494,15 +494,20 @@ public class RNFetchBlobFS {
494 494
      * @param paths An array of file paths.
495 495
      * @param callback JS contest callback
496 496
      */
497
-    static void removeSession(ReadableArray paths, Callback callback) {
497
+    static void removeSession(ReadableArray paths, final Callback callback) {
498 498
 
499 499
         AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {
500 500
             @Override
501 501
             protected Integer doInBackground(ReadableArray ...paths) {
502
-                for(int i =0; i< paths[0].size(); i++) {
503
-                    File f = new File(paths[0].getString(i));
504
-                    if(f.exists())
505
-                        f.delete();
502
+                try {
503
+                    for (int i = 0; i < paths[0].size(); i++) {
504
+                        File f = new File(paths[0].getString(i));
505
+                        if (f.exists())
506
+                            f.delete();
507
+                    }
508
+                    callback.invoke(null, true);
509
+                } catch(Exception err) {
510
+                    callback.invoke(err.getLocalizedMessage());
506 511
                 }
507 512
                 return paths[0].size();
508 513
             }

+ 14
- 0
src/class/RNFetchBlobSession.js View File

@@ -12,6 +12,8 @@ import {
12 12
 const RNFetchBlob = NativeModules.RNFetchBlob
13 13
 const emitter = DeviceEventEmitter
14 14
 
15
+let sessions = {}
16
+
15 17
 export default class RNFetchBlobSession {
16 18
 
17 19
   add : (path:string) => RNFetchBlobSession;
@@ -20,6 +22,18 @@ export default class RNFetchBlobSession {
20 22
   list : () => Array<string>;
21 23
   name : string;
22 24
 
25
+  static getSession(name:string):any {
26
+    return sessions[name]
27
+  }
28
+
29
+  static setSession(name:string, val:any) {
30
+    sessions[name] = val
31
+  }
32
+
33
+  static removeSession(name:string) {
34
+    delete sessions[name]
35
+  }
36
+
23 37
   constructor(name:string, list:Array<string>) {
24 38
     this.name = name
25 39
     if(!sessions[name]) {

+ 2
- 2
src/fs.js View File

@@ -68,11 +68,11 @@ function getSystemDirs() {
68 68
  * @return {RNFetchBlobSession}
69 69
  */
70 70
 function session(name:string):RNFetchBlobSession {
71
-  let s = sessions[name]
71
+  let s = RNFetchBlobSession.getSession(name)
72 72
   if(s)
73 73
     return new RNFetchBlobSession(name)
74 74
   else {
75
-    sessions[name] = []
75
+    RNFetchBlobSession.setSession(name, [])
76 76
     return new RNFetchBlobSession(name, [])
77 77
   }
78 78
 }