Porting a Windows Phone 8.0 Silverlight app to Windows Phone 8.1 Universal app - Database / SQLite

Two weeks ago I wrote about the preparation of porting my P | Cast app to Windows Phone 8.1 Universal.

After this I started investigating if I would be able to port my database including the data access code.

Prepare the Windows Phone project

So of course we need to have the SQLite database engine for Windows Phone 8.1. So download this Visual Studio Extensions: SQLite for Windows Phone 8.1. Now it’s time to add a reference to the SQLite engine on the Windows Phone project. It will also add a reference to the Microsoft.VCLibs.

While in the past a special C++ project would have to be added to your Windows Phone 8.0 solution, this is much easier.

image

Prepare the Windows Project

While I’m still not sure if the Windows version of P | Cast will ever see the light, I want to know if my code is compatible. So I downloaded the Visual Studio Extension: SQLite for Windows Runtime (windows 8.1). So this time add a reference to the SQLite engine on the Windows project.

image

Time for some Data Access

As always I try to decouple the data access from the UI. It’s a best practice I always try to follow. Nowadays we have Portable Class Libraries so I add a new PCL project to the solution.

image

I reference this project from both the Windows Phone and the Windows project.

Next I add the same NuGet package I’m used to use in the P | Cast app, called sqlite-net.

Everything compiles, time to migrate code. First I took the code from a table definition. Actually I copied all of my table definition classes, but for this article I’ll show the Subscription definition.

[Table("Subscriptions")]
public class Subscription
{
    [AutoIncrement, PrimaryKey]
    public int Id { get; set; }

    [Unique]
    public string Uri { get; set; }

    public string Title { get; set; }
    public string Description { get; set; }
    public string Copyright { get; set; }
    public string ArtUri { get; set; }

    public string ExternalId { get; set; }
    [Indexed]
    public DateTime LastUpdated { get; set; }
}

 

So I tried to do actually create the database, insert data and query the data. I called the below method from the OnLaunched.

private async void TryDatabaseStuff()
{
    var connection = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "podcasts.db"));
    await connection.CreateTablesAsync<Subscription, Track>();

    await connection.InsertAsync(new Subscription { Uri = "http://hanselminutes.com/subscribe" });
    Debug.Assert(await connection.Table<Subscription>().CountAsync() > 0);
}

 

So to assure you, all this code works like a charm on both Windows Phone and Windows. Now it’s time to migrate some more parts.

Porting a Windows Phone 8.0 Silverlight app to Windows Phone 8.1 Universal app - Preparation

As many of you readers probably know, I wrote a Podcast app called P | Cast. I did this in the early days of Windows Phone 8.0 because it lacked an internal podcast player (for most regions), and all the alternatives from the store weren’t working for me at that moment.

But then came Windows Phone 8.1, it included a podcast player, again. I thought this would be the death of my app. But it isn’t at least it isn’t for myself, I don’t like the internal podcast player that much. So I can of course expand P | Cast to become better, but one of the most requested features, modify playbackspeed, can’t be done on the Windows Phone 8.0 model. Time to port the P | Cast app to Windows Phone 8.1 Universal app. Not an easy task though.

User Interface

Yes both User Interfaces are in Xaml, but there are significant differences, in the controls that are available and also their richness. I’m currently using the Telerik UI for Windows Phone controls. They work perfectly right now, but I can’t use them on an universal app, and their Telerik UI for Windows Universal isn’t as rich as their Windows Phone counter part yet. On the other the current xaml can probably be a good base to start with.

Background Audio

In the Windows Phone 8.0 timeframe, all you have is a Background Audio Agent. There were a lot little quirks in the events that are or aren’t called in the Background Audio Agent. I had to do a lot to make sure track progress could be saved. Hope we don’t have to worry about that anymore. Even using NamedEvents to make sure the BackgroundAudio could somehow communicate with the UI.

The background audio architecture has changed a lot. It changed so much actually that I’m sure I will have to redo all the audio code. But all will come with a very interesting feature: the ability to change the playbackrate. So in a new version we will finally be able to play podcasts at 1.5 speed or 2.0 speed if you would like to.

Background Transfers

Because the sizes of podcasts can be very large I used the BackgroundTransferService to make sure the OS handles all the issues of resuming of transfers and even keep downloading if the app isn’t running in the foreground. Universal apps have a different API for handling Background Transfers which are very similar but have some differences in limitations it seems. I hope they can be started from a background task, which couldn’t be done on Windows Phone 8.0.

Background Processing

P | Cast includes a background agent which is responsible for syncing all podcast feeds and eventually clean up of played podcasts. When the phone is plugged in for charging an even more intense version of the background agent launches which will do background downloading of podcasts. Although the Background Task API is different for Universal apps, the concepts are similar. Just Run code on the background!

Database

All meta-data of the podcasts is stored in a SQLite database. I know you have to do something different to actually use SQLite in the app, but I hope I can still use SQLite-Net library to access the database.

Posted: Porting a Windows Phone 8.0 Silverlight app to Windows Phone 8.1 Universal app - Database / SQLite

Windows 8 app?

Yes the big question is of course, will there also be a Windows 8 app? Maybe there will be, there’s a lot of code that can be shared in Universal Apps. A new UI design will be required for a Windows 8 app of course. And every user would expect the Windows Phone and Windows apps to be in sync, always. That would require some additional thinking for me. Because I have no idea on how to do that, yet.

What’s next?

So I will write blogposts for each of the above mentioned things. I will explain what I needed to do to implement the different parts, and if possible if I could reuse code or not. If you have any tips for me, please share them in the comments.

Windows Store Apps, Error: The certificate specified has expired.

Today I tried to update one of my Windows Store Apps. When I tried to create an App Package for the Store I got an error. One that was completely new to me.

The certificate specified has expired. For more information about renewing certificates, see http://go.microsoft.com/fwlink/?LinkID=241478.

We’re lucky this time, renewing the certificate is very easy. Just follow below steps (copied from MSDN).

  1. In Solution Explorer, open the shortcut menu for the .appxmanifest file, choose Open With, and then choose App Manifest Designer.
  2. In the App Manifest Designer, choose the Packaging tab, and then choose the Choose Certificate button.
  3. In the Choose Certificate dialog box, expand the Configure Certificate list, and then choose Create test certificate.
  4. In the Create test certificate dialog box, click the OK button.

In the end Visual Studio will generate the new certificate with a new expiry date. The new certificate will be valid for a full year.

A small tale of bringing push notifications to Windows Azure

Some time ago I got more and more problems on my shared hosting because it couldn’t handle the amount of push notifications I wanted to send. I heard my hosting provider tell me that I was quite often taking more than 90% of the CPU on the server. So I thought to give Windows Azure a try.

I wanted to make optimal use of Windows Azure so I designed my solution to make use of Windows Azure Table Storage to store registered devices and pushchannels. When making use of Azure Table Storage it’s important to choose your RowKey and PartitionKey carefully.

To start with the PartitionKey, I chose to put the ApplicationName in there. I want to send push notifications to different applications I created, like Fokke & Sukke and iBood. So far I haven’t found a reason I made a wrong decision.

And now the RowKey, something that I need to use more carefully. The combination of PartitionKey and RowKey needs to be unique. So I wanted to put the DeviceId of the Device that should receive the PushNotification in there, that combined with the platform identifier. As far as I know there’s nothing that guarantees that the DeviceId is unqiue over different platforms. So I prefix the DeviceId with “WP|” for Windows Phone and “RT|” for Windows 8. The rest was just the copy of the DeviceId. I tested this using the emulator, and everything seems to work fine.

Windows Phone app trouble

After a while I notices reviews telling me that Push Notification don’t work, even further, it didn’t work on my own Windows Phone. After searching for many different reasons for this trouble, I found the source, more or less.

When saving an entity to the Azure Table Storage, every now and then a StorageException occurred. There aren’t many details in the exception, so after attaching Fiddler to my Nokia Lumia 920, I saw interesting stuff happening on the line.

The DeviceId contained special characters. I didn’t notice this when using the emulator, because the DeviceId on the emulator didn’t contain any special characters. So in total I had a percentage of users that could never register because of the StorageException, I still have no idea how large that percentage is, DeviceIds at least regularly contain the ‘/’ character.

Lesson learned, make sure the RowKey and PartitionKey don’t contain special characters: /, \, #, ?

Windows Style app trouble

Besides the Windows Phone issues, I had a very strange behavior on Windows 8 as well. It happened that I was sending a push notification to my local (installed through Visual Studio) app that did not appear. For example I sent a BadgeNotification with value 1 and sometimes the value 17 appeared. I have been trying to find the reason behind it, I never found it. Because when I tried to debug it explicitly with a value like 4 it did show 4. I never got feedback about issues with the push notifications on Windows 8 since my move to Windows Azure, but my dev-machine had troubles.

So after a couple of weeks trying to find causes for the problem I did something that was my final call. I did uninstall the app, and installed the app from the Store. What happened? The pushnotifications started behaving correctly. I have no understanding about the differences between the apps, but it’s good to be aware that there seem to be differences between the app installed from the store and the app installed by Visual Studio.

Your app, featured in the Store, WOW!

The first week of March, one of my Windows 8 apps got featured in the Dutch Windows 8 Store. There are a lot of things you can do to increase the amount of downloads your app is getting, but getting featured helps a lot. Normally the app get’s about 10 to 15 downloads a day, during the featured period it was around 110 each day (178 on March 3rd).

 

image

Of course the “how to get featured” is a big secret. We do know, not providing the promotional art during the submission process will not help your app getting featured.

If you have any tips, please share them in the comments.