Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

没有加一个超时处理,同时也会出现并发保存 object 的时候报错 #3

Closed
godblesshugh opened this issue Dec 21, 2016 · 8 comments

Comments

@godblesshugh
Copy link

多个文件同时保存,造成:
{"error":"Document not found"}
而且没有超时处理,导致以下代码不返回:

function saveAll(tempObjects) {
    var promises = [];
    for (var i = 0; i < tempObjects.length; i++) {
        promises.push(tempObjects[i].save());
    }
    return AV.Promise.all(promises);
}
@godblesshugh
Copy link
Author

并发保存的问题。在 LC 的 iOS 客户端也碰到了这个问题。

@leeyeh
Copy link
Member

leeyeh commented Dec 21, 2016

tempObjects 是什么?文件还是对象?

看起来你想批量保存一批对象,然后其中一个失败了(Document not found),整个 Promise 就失败了。

这是符合预期的啊,你需要 debug 下哪个对象是不存在的。

另外,AV.Object.saveAll 可以用在一个请求里批量保存对象,会更可靠(但是在这个 case 里肯定还是会 Document not found 失败)。

另外,你确定这个问题是 leantodo-weapp 的问题?

@godblesshugh
Copy link
Author

是一个文件。其实我的源码是类似这样的:

function saveAll(tempFilePaths) {
    var promises = [];
    for (var i = 0; i < tempFilePaths.length; i++) {
        promises.push(savePhoto(tempFilePaths[i]));
    }
    return AV.Promise.all(promises);
}

function savePhoto(tempFilePath) {
    var photo = new Photo();
    photo.set('photoFile', new AV.File('wxapp', {
        blob: {
            uri: tempFilePath
        },
    }));
    return photo.save();
}

然后,调用方式是这样的:

saveAll(tempFilePaths).then(null, function(err){
    // 错误处理
}).then(function(){
    // 不管成功失败,都刷新一下
    refresh();
});

因为这个 refresh() 始终没有被调用,所以才说是可能没有超时的处理。

@godblesshugh
Copy link
Author

然后我把 saveAll 去掉了,改成 reduce 的方式,强制改成顺序上传是可行的:

tempFilePaths.reduce(function(promise, tempFilePath) {
    return promise.then(function() {
        return savePhoto(tempFilePath);
    });
}, AV.Promise.resolve(true)).catch(function(err) {
    // 错误处理
}).then(function() {
    // 不管成功失败,都刷新一下
    refresh();
})

@leeyeh
Copy link
Member

leeyeh commented Dec 21, 2016

如果是并发上传的话,小程序有个同事 5 个请求的上线,可以看下控制台有没有类似的异常。我不确定这个异常是否会被正确 catch 到,我需要试一下。

@godblesshugh
Copy link
Author

OK,我这边是选择了两张图片同时做上传出现的。是在移动端出现,但是在小程序的那个 IDE 里面没有。
而且没有发现类似的异常,我的移动端是能一直重现的,改成 reduce 的时候倒是一切正常了。

@leeyeh
Copy link
Member

leeyeh commented Jan 5, 2017

论坛也有类似的多图上传失败的报告,我调查一下:

https://forum.leancloud.cn/t/topic/12790

@leeyeh
Copy link
Member

leeyeh commented Feb 7, 2017

Android 真机不支持并发上传图片。详情与解决方案参见上面的论坛帖子。

@leeyeh leeyeh closed this as completed Feb 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants