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

时间:2014-12-30 06:17:03

标签: c# windows-phone-8.1 onedrive

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

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

奇怪的是,在上传的过程中,我可以刷新我的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();
    }

1 个答案:

答案 0 :(得分:0)

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