SimonSimCity / Xamarin-CrossDownloadManager

A cross platform download manager for Xamarin
MIT License
149 stars 68 forks source link

Download never completed/finished (Xamarin Forms) #60

Closed Naografix closed 7 years ago

Naografix commented 7 years ago

Hello,

I followed the sample tutorial and I made this code:

public void DownloadMyFile()
{
    // If already downloading, abort it.
    if (downloadManager.IsDownloading()) {
        downloadManager.AbortDownloading ();
        Debug.WriteLine("Download aborted.");
        return;
    }

    downloadManager.InitializeDownload("http://ipv4.download.thinkbroadband.com/10MB.zip");

    downloadManager.File.PropertyChanged += (sender, e) => {

        var downloadFile = ((IDownloadFile)sender);
        switch (e.PropertyName) {
            case nameof(IDownloadFile.Status):
                Debug.WriteLine("Status : " + downloadFile.Status.ToString());
                break;
            case nameof(IDownloadFile.StatusDetails):
                Debug.WriteLine("StatusDetails : " + downloadFile.StatusDetails);
                break;
            case nameof(IDownloadFile.TotalBytesExpected):
                Debug.WriteLine("TotalBytesExpected : " + downloadFile.TotalBytesExpected.ToString());
                break;
            case nameof(IDownloadFile.TotalBytesWritten):
                Debug.WriteLine("TotalBytesWritten : " + downloadFile.TotalBytesWritten.ToString());
                break;
        }

        // Update UI if download-status changed.
        if (e.PropertyName == "Status") {
            switch (((IDownloadFile)sender).Status) {
            case DownloadFileStatus.COMPLETED:
            case DownloadFileStatus.FAILED:
            case DownloadFileStatus.CANCELED:
                Debug.WriteLine("Download finished"); 

                // Get the path this file was saved to. When you didn't set a custom path, this will be some temporary directory.
                //var nativeDownloadManager = (DownloadManager)ApplicationContext.GetSystemService (DownloadService);
                //System.Diagnostics.Debug.WriteLine (nativeDownloadManager.GetUriForDownloadedFile (((DownloadFileImplementation)sender).Id));

                // If you don't want your download to be listed in the native "Download" app after the download is finished
                //nativeDownloadManager.Remove(((DownloadFileImplementation)sender).Id);
                break;
            }
        }

        // Update UI while donwloading.
        if (e.PropertyName == "TotalBytesWritten" || e.PropertyName == "TotalBytesExpected") {
            var bytesExpected = ((IDownloadFile)sender).TotalBytesExpected;
            var bytesWritten = ((IDownloadFile)sender).TotalBytesWritten;

            if (bytesExpected > 0) {
                var percentage = Math.Round (bytesWritten / bytesExpected * 100);
                Debug.WriteLine("Downloading (" + percentage + "%)");
            }
        }
    };

    downloadManager.StartDownloading(false); 

}

But as you can see, I have a problem... (check my debug output)

[DownloadManager] Add -> New items: 1 at -1 || Old items: 0 at -1
TotalBytesExpected : -1
Status : PENDING
TotalBytesWritten : 65536
TotalBytesExpected : 1,048576E+07
Downloading (1%)
Status : RUNNING
TotalBytesWritten : 4521984
Downloading (43%)
TotalBytesWritten : 9371648
Downloading (89%)
Thread finished: <Thread Pool> #8
FINISHED

How can I fix this?

SimonSimCity commented 7 years ago

Sorry, but I haven't seen this before ...

Where does the FINISHED come from you have in the last line?

Could you please check out my repository and check if you also see it failing there? If no, please go on and remove the nuget package in the projects in your solution, include my projects and link them correspondingly. Does it now happen again? If yes you could try debugging from there on ...

Please also include what platform you tested this on.

Naografix commented 7 years ago

Hello,

Sorry, FINISHED in the last line is add by me to say "no more line after Thread..."

I tried your solution, it works, but your code is in Android project, not in PCL (Xamarin Forms).

I use Android to deploy my solution

SimonSimCity commented 7 years ago

My code is a PCL ... it's just not loaded as a nuget project. Since I haven't worked with Xamarin Forms before, I can only guess how to get you on with debugging.

I guess you have a project containing the shared code and one containing the Android specific stuff, right? Import the project Plugin.DownloadManager.Abstractions of this repository into your projects, both, where you have the shared code and there were the platform specific code goes. Import Plugin.DownloadManager.Android in your android specific project and Plugin.DownloadManager in the project where you have the shared code. Now you should be able to debug it all through.

There's also an introduction about PCL libraries (like this one is) on Xamarin, if you need more info: https://developer.xamarin.com/guides/cross-platform/application_fundamentals/pcl/introduction_to_portable_class_libraries/

Naografix commented 7 years ago

I have a Xamarin Forms solution with PCL not Shared project. I tried to switch my code (in my main post) in my Droid project and call it with the DependencyService. And... same... My download never finished.

EDIT : Weird, I do the same thing in a new project, and it works...

SimonSimCity commented 7 years ago

@Naografix does this mean you were able to solve this? Can I close the issue?

Naografix commented 7 years ago

@SimonSimCity Yes you can, thanks