- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 Windows Phone 8.1 512MB RAM 模拟器上,我的 WinJS 应用程序有时会出现奇怪的错误。我无法在其他模拟器实例或设备上重现它。
执行遍历 promise 链并完成以下 return 语句:
return ( currentUpload = uploadOperation.startAsync() );
在那之后什么也没有发生。我在赋予 .then 定义的所有三个函数(成功、失败、挂起)处设置了断点。当这种奇怪的情况发生时,这三个功能代码都不会到达。
我也将此 return 语句放在 try catch block 中,但没有捕获异常。
代码简要说明:
Background Uploader 实例已创建(自定义 header + PUT 方法)
StorageFile通过URI打开
后台 uploader 准备上传该文件(uploadOperation 的定义)
将开始上传操作
查看完整代码:
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 类上使用 group/TransferGroup,但我也收到了已弃用的警告:
The method Windows.Networking.BackgroundTransfer.IBackgroundTransferBase.put_Group has been deprecated. Group may be altered or unavailable for releases after Windows 8.1. Instead, use TransferGroup.
可能与那个 promise 链错误有关。
最佳答案
也许是一个简单的打字错误??
只需检查下面代码s中的注释
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 ???
*/
为了确定这一点,它可能遗漏了您的代码的一部分,因为在这种状态下您的代码必须中断!
也许调整可能是:
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');
}
);
}
编辑
进一步(我不使用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 = '已取消'
您正在检查,但我很确定这部分代码永远不会到达。
在此之前你拒绝你的 promise :返回 WinJS.Promise.wrapError('找不到文件');
也许下一个 then
应该处理这样的事情:
.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());
}
)
希望对您有所帮助。
关于javascript - WinJS:在 512MB WinPhone8.1 Emu 上使用 BackgroundUploader 类后,Promise 链有时会卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33232043/
我正在我的 xamarin.forms 应用程序中实现扫描仪功能,为此我正在使用 iOS native AVCaptureSession。但我的问题是在扫描或捕获 session 处于事件状态并且设备
所以我目前正在为我的项目制作一个音乐应用程序,它允许用户创建自己的音乐播放列表。但是,当我单击显示媒体选择器按钮时,它只显示白屏,当包含媒体选择器的 View 是 Initial View Contr
当我尝试在模拟器中启动 AVD 时,会出现一个小窗口(见图片),5 秒后它说没有响应并一直保持这种状态直到我关闭它。 我在网上搜索并尝试了所有解决方案,但都没有成功 在 BIOS 中启用了虚拟化 已安
尝试使用以下命令从视频中提取特定帧(删除了文件的特定名称!: ffmpeg -i video.mp4 -vf "select-gte(n\,6956)"-vframes 10262 文件夹/帧%d.j
我怎么知道终端正在继续工作而不中断它? 我已经运行了以下 git 命令: clone git://ligo-vcs.phys.uwm.edu/lalsuite.gituote 一段时间后它似乎被卡住了
我对 WPF 中的数据网格有一个奇怪的问题。我正在为我的应用程序使用 MVVM 模式,并且我的 View 模型实现了 idataerrorinfo 接口(interface)。每当我在添加新行后在我的
我有这个 Excel 文件,当我输入数据时它卡住了。例如,我双击一个单元格,输入数据,然后按“输入”。它会卡住而不是进入下面的细胞。按几次“enter”不会解冻程序,唯一有效的是用鼠标选择另一个单元格
我有线程池的任务队列,每个任务都有卡住锁定其正在使用的所有资源的倾向。并且除非重新启动服务,否则这些无法释放。 ThreadPool 中有没有办法知道它的线程已经被卡住?我有一个使用超时的想法(虽然我
我制作了以下小程序来确定内存是否用于 freeze(X,Goal) 之类的目标回收时 X变得无法访问: %:- use_module(library(freeze)). % Ciao Prolog n
我有一个使用 swing 的简单 java 应用程序。然而,当我执行程序时,框架将会出现,但我无法单击任何地方,并且按钮仅在几秒钟后出现。我对 javas Swing 库非常陌生,所以我可能会丢失一些
我正在尝试创建一个简单的 TCP 客户端服务器应用程序接口(interface)用户可以在按下相应按钮时启动或停止服务器我创建了一个 StartServer 按钮,当用户按下按钮时它应该连接到服务
我正在尝试从 ftp 服务器下载文件,但在检索文件时卡住了。我正在使用 commons-net-3.6.jar 我注意到的事情 当我使用 ftpClient.enterRemotePassiveMod
我正在尝试编写一个函数,该函数将能够找到位于我系统上的可执行文件搜索路径中的任意可执行文件。我遇到了一些输入会导致 SearchPathW 的问题无限期地卡住,我不确定到底发生了什么。 std::op
我的 Nativescript 应用程序的许多页面中都有 RadSideDrawer。主应用程序组件有一个 page-router-outlet并且所有其他页面都通过导航加载到此组件中。带抽屉的页面包
我有一个最小的服务器,它等待客户端连接,然后他启动一个线程,将回复发送回客户端,问题是回复。 这是服务器的代码: int port = 1234; ServerSocket servSock =
我有一个使用 C# 的 WinForms 应用程序。我尝试从文件中读取一些数据并将其插入到数据表中。虽然此操作很忙,但我的表单卡住并且我无法移动它。有谁知道我该如何解决这个问题? 最佳答案 这可能是因
在我们学校最新的项目中,我遇到了一些问题。我想观察新条目的路径,该路径是由文件导向器按钮选择的,但如果我选择任何文件,整个窗口都会卡住...我猜它被卡住,因为调用了“observePath”方法,但我
当我输入一百万作为输入数字时,我的程序卡住了。我该如何解决这个问题? 我尝试将第二个 for 循环分离为第二个函数,但没有成功。 import java.io.*; public class Arra
早上好编译我的应用程序时,我在 Android Studio 上遇到问题。我在构建时没有收到关于 app:transformClassesWithDexBuilderForDebug 的任何输出错误,
我正在使用以下触发器 DELIMITER ; CREATE TRIGGER updateCount AFTER INSERT ON user_info FOR EACH ROW BEGIN UPDA
我是一名优秀的程序员,十分优秀!