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
      * @param paths An array of file paths.
494
      * @param paths An array of file paths.
495
      * @param callback JS contest callback
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
         AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {
499
         AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {
500
             @Override
500
             @Override
501
             protected Integer doInBackground(ReadableArray ...paths) {
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
                 return paths[0].size();
512
                 return paths[0].size();
508
             }
513
             }

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

12
 const RNFetchBlob = NativeModules.RNFetchBlob
12
 const RNFetchBlob = NativeModules.RNFetchBlob
13
 const emitter = DeviceEventEmitter
13
 const emitter = DeviceEventEmitter
14
 
14
 
15
+let sessions = {}
16
+
15
 export default class RNFetchBlobSession {
17
 export default class RNFetchBlobSession {
16
 
18
 
17
   add : (path:string) => RNFetchBlobSession;
19
   add : (path:string) => RNFetchBlobSession;
20
   list : () => Array<string>;
22
   list : () => Array<string>;
21
   name : string;
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
   constructor(name:string, list:Array<string>) {
37
   constructor(name:string, list:Array<string>) {
24
     this.name = name
38
     this.name = name
25
     if(!sessions[name]) {
39
     if(!sessions[name]) {

+ 2
- 2
src/fs.js View File

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