You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by GitBox <gi...@apache.org> on 2018/12/12 05:42:43 UTC

[GitHub] shiftj18 opened a new issue #1895: callback of storage.getAllKeys not be called when storage is empty.

shiftj18 opened a new issue #1895: callback of storage.getAllKeys not be called when storage is empty.
URL: https://github.com/apache/incubator-weex/issues/1895
 
 
   ## Describe the bug
   callback of storage.getAllKeys not be called when storage is empty.
   
   
   ## To Reproduce
   1. removeAllItems, by getAllKeys and removeItem.
   2. getAllKeys again, oops; callback is not called.
   
   
   ## Environment
   * Device: pc simulator
   * OS: mac
   * Version [e.g. 22]
   * Build from source [e.g. yes/no]
   
   
   ## Expected behavior
   the callback will be called, even if storage is empty. so, i can know getAllKeys is finished.
   
   
   ## Additional context
   code like: 
   `clearLocalStorage() and then readLocalStorage(), branch1 & branch2 both can be called.
   
   ```js
     readLocalStorage () {
       let storageList = [];
   
       const readAllFinish = () => {
         this.popModal({
           data: storageList
         });
       }
   
       AsyncStorage.getAllKeys(obj => {
         if (obj.result === 'success') {
           const { data: keys } = obj;
           keys.map((key, kIdx) => {
             AsyncStorage.getItem(key, (e) => {
               if (e.result === 'success') {
                 storageList.push({
                   key,
                   value: e.data
                 })
               }
               if (kIdx == keys.length - 1) {
                 readAllFinish();
               }
             })
           })
           if (!keys.length) { //branch2
             readAllFinish();
           }
         } else {   //branch1
           readAllFinish();
         }
       })
       // hack: callback of getAllKeys not call when sotrage is empty.
       setTimeout(() => {
         if (!storageList.length) {
           readAllFinish();
         }
       }, 200);
     }
   
     clearLocalStorage () {
       let failList = [];
   
       const clearAllFinish = () => {
         const failtips = `以下清除失败: ${failList.map(k => ('\n' + k))}`;
         toast('清空LocalStorage完成.', 400);
         if (failList.length) {
           toast(failtips, 3000);
         }
       }
   
       AsyncStorage.getAllKeys(obj => {
         if (obj.result === 'success') {
           
           const { data: keys } = obj;
           keys.map((key, kIdx) => {
             !!key && AsyncStorage.removeItem(key, (e) => {
               // remember failed key.
               if (e.result !== 'success') {
                 failList.push(key);
               }
             })
             if (kIdx == keys.length - 1) {
               clearAllFinish();
             }
           })
         }
       })
     }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services