OneDrive 文件仅在应用终止时上传

本文关键字:终止 应用 文件 OneDrive | 更新日期: 2023-09-27 18:35:34

我在从通用应用程序将文件上传到OneDrive时遇到问题,我无法理解或弄清楚如何调试。我使用本指南完成了获取文件 ID 等的过程,直到几个小时前它都运行良好。

现在我可以获取文件夹和文件 ID,因此我假设我仍然成功连接到 OneDrive,并且我的互联网连接仍在工作。但是当我进入 BackgroundUploadAsync 时,线程或之前正在执行的任何内容都不会返回。在下面的代码中,消息"正在将新文件上载到 OneDrive..."永远不会消失。

奇怪的是,当它正在上传时,我可以在ie上刷新我的OneDrive文件夹,但我永远不会看到我试图上传的内容。但是一旦我停止调试器,或终止手机上的应用程序,我就可以立即刷新,文件就会在那里。

以下是上传方法:

public async Task UploadToOneDrive(string folderID, string localFileName)
    {
        try
        {
            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(localFileName);
            string fileName = "backup-" + DateTime.Now.ToString("dd-MM") + ".db";
            await file.CopyAsync(ApplicationData.Current.LocalFolder, fileName, NameCollisionOption.ReplaceExisting);
            file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
            await connectClient.BackgroundUploadAsync(folderID,
                fileName, file, OverwriteOption.Overwrite);
        }
        catch (LiveConnectException)
        {
            MessageDialog m = new MessageDialog("Could not connect to to OneDrive. Cloud sync will be stopped.");
            m.ShowAsync();
        }
        catch (LiveAuthException)
        {
            MessageDialog m = new MessageDialog("Error authenticating OneDrive service. Please try cloud sync again later.");
            m.ShowAsync();
        }
        catch (Exception ex)
        {
            MessageDialog m = new MessageDialog("Unknown exception occurred.'n'nError:{0}", ex.Message);
            m.ShowAsync();
        }
    }

这是同步过程

public async Task sync()
    {
        var sb = StatusBar.GetForCurrentView();
        sb.ProgressIndicator.Text = "Syncing with OneDrive...";
        await sb.ProgressIndicator.ShowAsync();
        string cloudFolderID = await syncManager.CreateOrGetOneDriveFolderID("GlucoseCalculator", "Documents/");
        string cloudFileID = await syncManager.GetFileID(DataManager.sqlFileName, "Documents/GlucoseCalculator/");
        try
        {
            if (cloudFileID != null)
            {
                if (!(await dbManager.DoesFileExist(DataManager.sqlFileName)))
                {
                    sb.ProgressIndicator.Text = "Downloading file from OneDrive...";
                    await syncManager.DownloadFromOneDrive(cloudFileID, DataManager.sqlFileName);
                    goto BREAK;
                }
                DateTime cloudLastEditDateTime = DateTime.Parse(await syncManager.GetFileProperty(cloudFileID, "updated_time"));
                DateTime localLastEditDateTime = ApplicationData.Current.LocalFolder.GetFileAsync(DataManager.sqlFileName).GetResults().GetBasicPropertiesAsync().GetResults().DateModified.DateTime;
                if (cloudLastEditDateTime > localLastEditDateTime)
                {
                    sb.ProgressIndicator.Text = "Downloading file from OneDrive...";
                    await syncManager.DownloadFromOneDrive(cloudFileID, DataManager.sqlFileName);
                }
                else if (cloudLastEditDateTime < localLastEditDateTime)
                {
                    sb.ProgressIndicator.Text = "Uploading file to OneDrive...";
                    await syncManager.UploadToOneDrive(cloudFolderID, DataManager.sqlFileName);
                }
            }
            else if (cloudFileID == null)
            {
                sb.ProgressIndicator.Text = "Uploading new file to OneDrive...";                                
                await syncManager.UploadToOneDrive(cloudFolderID, DataManager.sqlFileName);
            }
        }
        catch (Exception)
        {
            sb.ProgressIndicator.Text = "Cloud synchronization failed.";
            sb.ProgressIndicator.HideAsync();
            return;
        }
        sb.ProgressIndicator.Text = "Synchronization complete!";
        BREAK:;
        await sb.ProgressIndicator.HideAsync();
    }

OneDrive 文件仅在应用终止时上传

最有可能的是,您正在创建一些实现IDisposible的对象,但它不在using块中。也许是StorageFile.