2015-10-20 6 views
5

पर पृष्ठभूमिउप्लोडर वर्ग के उपयोग के बाद कभी-कभी वादा श्रृंखला चिपक जाती है मुझे अजीब त्रुटि होती है जो कभी-कभी विंडोज फोन 8.1 512 एमबी रैम एमुलेटर पर मेरे विनजेएस ऐप पर होती है। मैं इसे अन्य एमुलेटर उदाहरणों या डिवाइस पर पुन: पेश नहीं कर सका।विनजेएस: 512 एमबी WinPhone8.1 Emu

निष्पादन वादा श्रृंखला के माध्यम से चलाता है और निम्नलिखित वापसी कथन खत्म:

return (currentUpload = uploadOperation.startAsync()); 

उसके बाद कुछ नहीं होता। मैंने .थ परिभाषा को दिए गए सभी तीन कार्यों (सफलता, विफलता, लंबित) पर ब्रेकपॉइंट सेट किए हैं। यह अजीब मामला तब होता है जब इनमें से कोई भी तीन फ़ंक्शन कोड कभी नहीं पहुंचाया जाएगा।

मैंने इस वापसी कथन को एक कोशिश पकड़ ब्लॉक पर भी रखा लेकिन पकड़ने के लिए कोई अपवाद नहीं था।

कोड का संक्षिप्त विवरण:

  • पृष्ठभूमि अपलोडर उदाहरण बनाई गई है (कस्टम हेडर + PUT विधि)

  • StorageFile यूआरआई

    द्वारा खोला जाता है
  • पृष्ठभूमि अपलोड करने वाले उस फ़ाइल का अपलोड तैयार करता है (अपलोडऑपरेशन की परिभाषा)

  • अपलोडऑपरेशन शुरू किया जाएगा

    var currentUpload; // global 
    
    function uploadFile(localFullPath, headers, serverUrl) 
    { 
        var fileUri = new Windows.Foundation.Uri('ms-appdata:///local' + localFullPath), 
         uploader = false; 
    
         try 
         { 
          uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader(); 
          uploader.method = 'PUT'; 
    
          // set headers to uploader 
          for (var key in headers) 
          { 
           if (headers.hasOwnProperty(key)) 
            uploader.setRequestHeader(key, headers[key]); 
          } 
         } 
         catch (e) 
         { 
          // error handling 
          return false; 
         } 
    
         Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileUri) 
         .then(function success(file) 
         { 
          return uploader.createUpload(serverUrl, file); 
         }, 
         function failure(error) 
         { 
          return WinJS.Promise.wrapError('file not found'); 
         }) 
    
         .then(function (uploadOperation) 
         { 
          if (currentUpload == 'Canceled') 
           return WinJS.Promise.wrapError('upload canceled'); 
          else 
           return (currentUpload = uploadOperation.startAsync()); 
    
         }) 
    
         .then(function success(success) 
         { 
          currentUpload = false; 
          // success handling 
          return true; 
         }, function failure(error) 
         { 
          currentUpload = false; 
          // error handling 
          return false; 
         } 
    
         }, function pending(status) 
         { 
          var progress = status.progress, 
           percent = Math.round(progress.bytesSent/progress.totalBytesToSend * 100); 
    
          // progress handling 
         }); 
         } 
    

    किसी भी मदद के लिए धन्यवाद:

पूर्ण कोड देखो!

पीएस मैं भी एक पदावनत चेतावनी मिल गया, हालांकि मैं BackgroundUploader वर्ग पर समूह/TransferGroup का उपयोग नहीं कर रहा हूँ:

विधि Windows.Networking.BackgroundTransfer.IBackgroundTransferBase.put_Group मान्य नहीं है। Windows 8.1 के बाद रिलीज़ के लिए समूह को बदला या अनुपलब्ध किया जा सकता है। इसके बजाय, ट्रांसफर समूह का उपयोग करें।

शायद यह उस वादे श्रृंखला त्रुटि से संबंधित है।

+1

मैं स्पष्ट रूप से समझ नहीं पा रहा हूं कि आप 'वापसी (currentUpload = uploadOperation.startAsync()) के साथ क्या हासिल करने का प्रयास कर रहे हैं; '। इसके अलावा आप गड़बड़ कर चुके हैं आप चेनिंग का वादा करते हैं। जब वादे का सामना करना पड़ता है, तो मुझे अंत में और उन सभी के लिए त्रुटि प्रबंधन करना चाहिए। यह वादा करने की वादा की सुंदरता है। – sebagomez

+0

यह कथन वर्तमान अपलोड को वैश्विक चर में सहेजता है। कल्पना करें कि यह अपलोड वापस आएगाऑपरेशन.स्टार्टएसिंक() - इसका परीक्षण किया गया, त्रुटि तब भी हुई। "गड़बड़" के साथ आपका क्या मतलब है? क्या आप सर्वोत्तम अभ्यास और शैली के बारे में बात कर रहे हैं या इससे कोई त्रुटि हो सकती है? कारण मैंने ऐसा किया कि उसने मुझे केवल "WinRT त्रुटि" दी, इसलिए मैं त्रुटियों के बीच उचित रूप से अंतर नहीं कर सका (एक पाठ है लेकिन यह एक विशिष्ट भाषा के लिए बाध्य है)। – kerosene

+0

[एमएसडीएन दस्तावेज़ीकरण] के अनुसार (https://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.backgroundtransfer.downloadoperation.startasync.aspx), वह फ़ंक्शन प्रदर्शन को घटा सकता है। शायद आप मंदी का सामना कर रहे हैं। वे अनुशंसा करते हैं कि "आप पृष्ठभूमि कार्यकर्ता थ्रेड पर StartAsync को कॉल करें ..." क्या यह आपकी समस्या का स्रोत हो सकता है? – Jimmy

उत्तर

2

शायद एक साधारण टाइपो त्रुटि ??
बस नीचे दिए गए

var currentUpload; // global 

function uploadFile(localFullPath, headers, serverUrl) 
{ 
    var fileUri = new Windows.Foundation.Uri('ms-appdata:///local' + localFullPath), 
     uploader = false; 

     try 
     { 
      uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader(); 
      uploader.method = 'PUT'; 

      // set headers to uploader 
      for (var key in headers) 
      { 
       if (headers.hasOwnProperty(key)) 
        uploader.setRequestHeader(key, headers[key]); 
      } 
     } 
     catch (e) 
     { 
      // error handling 
      return false; 
     } 

     /* 

     return something it's a good practice if you want to chain Promises 
      | 
      | 
      V                  */ 
     return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileUri) 
     .then(
      function success(file){ 
      return uploader.createUpload(serverUrl, file); 
      }, 
      function failure(error){ 
      return WinJS.Promise.wrapError('file not found'); 
    //          |_____________| 
    // your are rejecting your promise with ---------^ 
      } 
     ).then(
      function (uploadOperation){ 

    // Just avoid this type of code construction 
    // and add brace ! 
    // adding brace augment readability and prevent errors ! 

      // where do you set this ??? currentUpload = 'Canceled' 
      // is part of winjs ??? 
      if (currentUpload == 'Canceled') 

      // why not handle your rejected promise ? 
      // by something like : 
      //if(uploadOperation == 'file not found') 
       return WinJS.Promise.wrapError('upload canceled'); 
      else 
       return (currentUpload = uploadOperation.startAsync()); 
      } 
     ).then(
// Promise resolve handler 
      function success(success){ 
      currentUpload = false; 
      // success handling 
      return true; 
      } , 
// Promise reject handler 
      function failure(error){ 
      currentUpload = false; 
      // error handling 
      return false; 
      } 
/*  ^ 
      | 
YOU HAVE HERE A DOUBLE CLOSING BRACE }} ??? 
° 
| WHAT IS THIS PART FOR ?????? 
| 
+------+---- Ending uploadFile with successive closing brace ! 
     | 
     |+---------------- Declaration separator 
     ||  +--- New function declaration 
     ||  | 
     VV  V      */ 
     }, function pending(status){ 
      var progress = status.progress, 
       percent = Math.round(progress.bytesSent/progress.totalBytesToSend * 100); 
      // progress handling 
     }); 
     } 

/* 
ALL THAT PART ABOVE IS NOT IN THE PROMISE CHAIN 
AND SHOULD BREAK YOUR CODE !!! 
HOW HAVE YOU EVER COULD RUN THIS ??? 
*/ 

कोड रों में टिप्पणियां देखनी शायद यह अपने कोड का एक हिस्सा याद आती है, क्योंकि इस राज्य में अपने कोड को तोड़ने चाहिए, इस बात का यकीन है कि हो सकता है!


हो सकता है कि एक adjustement हो सकता है:

var currentUpload; // global 

function uploadFile(localFullPath, headers, serverUrl) 
{ 
    var fileUri = new Windows.Foundation.Uri('ms-appdata:///local' + localFullPath), 
     uploader = false; 

    try 
    { 
    uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader(); 
    uploader.method = 'PUT'; 

    // set headers to uploader 
    for (var key in headers) 
    { 
     if (headers.hasOwnProperty(key)) 
     uploader.setRequestHeader(key, headers[key]); 
    } 
    } 
    catch (e) 
    { 
    // error handling 
    return false; 
    } 

    /* 

    return something it's a good practice if you want to chain Promises 
    | 
    | 
    V                  */ 
    return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileUri) 
    .then(
    function success(file){ 
     return uploader.createUpload(serverUrl, file); 
    }, 
    function failure(error){ 
     return WinJS.Promise.wrapError('file not found');// O|--------+ 
    }//                | 
).then(//               | 
    function (uploadOperation){// O|-------------------------------| 
     //               | 
     // Just avoid this type of code construction     | 
     // and add brace !           | 
     // adding brace augment readability and prevent errors !  | 
     //               | 
     // EDIT              | 
     //if (currentUpload == 'Canceled') { // <--- add brace  | 
     if (uploadOperation == 'file not found') { //<---add brace <--+ 

     return WinJS.Promise.wrapError('upload canceled'); 

     } else { // <---- add braces 

     // even if it is valid 
     // avoid assignement like that 
     // so subtil to read/understand/debug ... 
     // return (currentUpload = uploadOperation.startAsync()); 

     currentUpload = uploadOperation.startAsync(); 
     // maybe debug here ? 
     console.log('currentUpload : ' + currentUpload); 
     return currentUpload; 

     } // <---- add brace 
    } 
).then(
    function success(success){ 
     currentUpload = false; 
     // success handling 
     return true; 
    } , 
    function failure(error){ 
     currentUpload = false; 
     // error handling 
     return false; 
    } , 
    function pending(status){ 
     var progress = status.progress, 
      percent = Math.round(progress.bytesSent/progress.totalBytesToSend * 100); 
     // progress handling 
    } 
).done(// it is always a good thing to have a final catcher !! 
    function(){ 
     // things are ok ! 
     console.log('ok'); 
    }, 
    function(err){ 
     // make something with your error better than 
     alert(err); 
     console.log('ko'); 

    } 
); 
} 

संपादित

खोज रहे हैं आगे (i winjs उपयोग न करें), वहाँ कुछ स्पष्ट किया जा सकता है:

.then(
    function success(file) { 
    return uploader.createUpload(serverUrl, file); 
    }, 
    function failure(error) { 
    return WinJS.Promise.wrapError('file not found'); 

    } 
).then(
    function(uploadOperation) { 

    if (currentUpload == 'Canceled') 
     return WinJS.Promise.wrapError('upload canceled'); 
    else 
     return (currentUpload = uploadOperation.startAsync()); 
    } 
). 

आप इसे कहां सेट कर रहे हैं:
currentUpload = 'Canceled'
आप इसे जांच रहे हैं, लेकिन मुझे यकीन है कि कोड का यह हिस्सा कभी नहीं पहुंचा है।
कि इससे पहले कि आपके साथ अपने वादा अस्वीकार:
return WinJS.Promise.wrapError('file not found');
शायद अगली then उस तरह someting संभाल चाहिए:

.then(
    function success(file) { 
    return uploader.createUpload(serverUrl, file); 
    }, 
    function failure(error) { 
    return WinJS.Promise.wrapError('file not found'); 

    } 
).then(
    function(uploadOperation) { 

    if (uploadOperation == 'file not found') 
     return WinJS.Promise.wrapError('upload canceled'); 
    else 
     return (currentUpload = uploadOperation.startAsync()); 
    } 
) 

मुझे आशा है कि यह आप में मदद मिलेगी।

+0

आपके उत्तर के लिए धन्यवाद। लंबित (स्थिति) फ़ंक्शन एक प्रगति फ़ंक्शन है। 'वादा। (पूर्ण, ऑनर, ऑन प्रोग्रेस) .डोन (/ * आपकी सफलता और त्रुटि हैंडलर * /);' [एमएस डॉकू] (https://msdn.microsoft.com/de-de/library/windows /apps/br229728.aspx) मुझे लगता है कि .catch() WinJS पर उपलब्ध नहीं है - कम से कम मुझे कोई दस्तावेज नहीं मिला। मुझे लगता है कि .done भाग पर एक विफलता समारोह यह काम करना चाहिए। मैंने पहले ही उन्हें परिभाषित करने की कोशिश की है, लेकिन त्रुटि अभी भी होती है। – kerosene

+1

मैंने अपना जवाब अपडेट कर लिया है, मुझे लगता है कि आपके पास गलत जगह पर एक डबल क्लोजिंग ब्रेस है जो इस खराब व्यवहार को जन्म देता है – Anonymous0day

+1

मैंने एक बार फिर से अपना जवाब संशोधित किया है, केवल टिप्पणियों की जांच करें, कणों को 'फ़ंक्शन (अपलोडऑपरेशन) के बारे में बताएं। ' – Anonymous0day

संबंधित मुद्दे