IT tutorials
 
Technology
 

Windows Phone 8 : Background File Transfer Sample Code - Restoring the Local Database

3/23/2014 9:26:05 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Restoring the local database involves submitting a background transfer request to download the previously uploaded file from the server. The file is downloaded to a temporary location in isolated storage, the existing local database is disconnected, and its file is replaced.

The RestoreDatabase method begins by creating a temporary directory where the downloaded .sdf file can be placed by the BackgroundTransferService:

const string downloadPath = transferDirectory + "/" + localDatabaseName;

using (IsolatedStorageFile isolatedStorageFile
            = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (!isolatedStorageFile.DirectoryExists(transferDirectory))
    {
        isolatedStorageFile.CreateDirectory(transferDirectory);
    }
}

It then creates two Uri objects specifying the location of the .sdf file on the remote server, and the file’s destination location in isolated storage, as shown:

string deviceId = GetUserId();

string remoteUrl = string.Format("{0}{1}_{2}",
                                    downloadUrl,
                                    deviceId,
                                    localDatabaseName);
Uri remoteUri = new Uri(remoteUrl, UriKind.Absolute);
Uri localUri = new Uri(downloadPath, UriKind.Relative);

The BackgroundTransferRequest is constructed using the two Uri objects. The default HTTP method Get is used because we are downloading the file to the device. See the following excerpt:

BackgroundTransferRequest request
    = new BackgroundTransferRequest(remoteUri, localUri)
    {
        TransferPreferences = TransferPreferences.AllowBattery,
    };

Finally, we subscribe to the transfer request’s status changed and progress changed events, and the request is added to the BackgroundTransferService. The progress indicator is displayed, and it is updated as the progress of the background transfer changes:

request.TransferStatusChanged += HandleDownloadTransferStatusChanged;
request.TransferProgressChanged += HandleDownloadTransferProgressChanged;

BackgroundTransferService.Add(request);
Message = "Restoring data from cloud.";
Progress = 0;
ProgressVisible = true;

When the background transfer completes, the TransferStatusChanged event handler is called (see Listing 1). The downloaded file is copied to the location of the local database, which replaces the existing file. The ITodoService.Initialize method re-creates the connection to the database, and the viewmodel’s GroupedTodoItems are re-created via a call to PopulateItems.

LISTING 1. HandleDownloadTransferStatusChanged Method


void HandleDownloadTransferStatusChanged(object sender,
                                         BackgroundTransferEventArgs e)
{
    if (e.Request.TransferStatus == TransferStatus.Completed)
    {
        BackgroundTransferService.Remove(e.Request);

        ProgressVisible = false;

        if (e.Request.TransferError != null)
        {
            MessageService.ShowError("An error occured during restore.");
            return;
        }

        try
        {
            using (IsolatedStorageFile isolatedStorageFile
                         = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string downloadedFile
                          = e.Request.DownloadLocation.OriginalString;
                isolatedStorageFile.CopyFile(downloadedFile,
                                             localDatabaseName, true);
            }

            todoService.Initialize();

            ClearPinnedItems();
            PopulateItems();
        }
        catch (Exception ex)
        {
            MessageService.ShowError("An error occured during restore.");
            return;
        }

        MessageService.ShowMessage("Restore successful.");
    }
}


Backup and restore operations are actuated by application bar menu items in the view. The view’s AppBar menu items are bound to the BackupDatabaseCommand and the RestoreDatabaseCommand (see Figure 1).

Image

FIGURE 1 The TodoListView provides menu items for backing up and restoring the local database, and a progress indicator that shows the progress of the background transfer request.

Using a BackgroundTransferRequest is an effective way to back up your apps’ data because it does not rely on your foreground app being active. Be mindful, however, that the BackgroundTransferService does not guarantee that a transfer request will be serviced.

 
Others
 
- Windows Phone 8 : Background File Transfer Sample Code - TodoListViewModel, Backing Up the Local Database
- Windows Phone 8 : Background File Transfer Sample Code - Retrieving the User’s Windows Live Anonymous ID
- Windows Phone 8 : Background File Transfer Sample Code - Using URL Rerouting with a WCF Service
- Windows Phone 8 : Performing Background File Transfers - Background Transfer Requests
- Microsoft Lync Server 2013 : Lync Server 2013 Mobility Technical Review - Push Notifications
- Microsoft Lync Server 2013 : Lync Server 2013 Mobility Technical Review - Sign-In
- Microsoft Lync Server 2013 : Lync Server 2013 Mobility Technical Review - Understanding the LyncDiscover Service, Understanding the Mobility Service
- Microsoft Lync Server 2013 : Mobile Clients Overview
- Active Directory 2008 Optimization and Reliability : Saving and Analyzing Data with Performance Logs and Alerts
- Active Directory 2008 Optimization and Reliability : Using Windows Server 2008 Performance Tools (part 3) - Managing Performance Monitor Properties
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us