WP7 Tutorial : 03 - Isolated Storage

Sep 13, 2012 Posted by Lara Kannan

Introduction


Isolated Storage aims to enable managed applications to create and maintain local storage. In Windows Phone 7 Silverlight-based applications, all I/O operations are restricted to isolated storage and do not have direct access to the underlying OS file system. Windows Phone application developers owe the ability to store data locally on the phone, leveraging all the benefits of isolated storage, such as providing security and preventing unauthorized access and data corruption from other applications.

In Windows Phone 7 programming Isolated Storage manipulation is as important as memory handling in traditional desktop applications. So, in this article we are still going to focus Isolated Storage.

Introducing Isolated Storage


To store local data a on a Windows Phone 7 phone, the tool of choice is isolated storage.  There are several efforts to create Windows Phone 7 databases, but in the end, these all run on top of isolated storage.

Isolated Storage is not new to Windows Phone, but  has been around a while in the Silverlight world.  And while it might sound fancy, isolated storage is just a clever way of saying “Write your data to text files.” 

We say Files and Folders are the building blocks for any application. We need to store data in our Phones to persist data when the application is not running. In case of Windows Phone 7 microsoft provides a secure way to store data into Isolated Store.

Isolated Storage, as the name suggests is a special virtualized file system that every application can access for standard IO operations yet the file is unavailable to any other application. Hence the files stored by one application is isolated from another.

Each application has a root of the store of this Virtualized File system. You can use the store to create folders and files. The main advantage of the Isolated store is independence between the actual file system and the application. Hence it gives a strong decoupling between the actual physical architecture of the system and the application. To understand, lets see the image below :

For Silverlight applications in Windows Phone 7, data may exist in several forms. For example, read-only data can be stored in a local file within your application.

User-specific data can be stored locally in an area called Isolated Storage. Data can also be stored on the Internet, which can be accessed through Web services.


Figure 1: Data Source in WP7 Silverlight App

The aim of designing Isolated Storage is to make each Windows Phone applications handle its own independent memory area, not to be affected by other Applications. In this way, applications can only focus upon the development of their own data.

If you need to store and retrieve user-specific information, you might consider using isolated storage.

There are typically two ways to use isolated storage. The first way is to save or retrieve data as key/value pairs by using the IsolatedStorageSettings class. The second way is to save or retrieve files by using the IsolatedStorageFile class.

Figure 2: Two ways to use isolated storage in WP7 Silverlight App


Basically in Silverlight for Windows Phone 7 you can store data in:

  • IsolatedStorageSettings

  • Folders and files in the Isolated Storage

  • Database

6 Things you need to consider when using Isolated Storage


  • 1. "Isolated"  storage means that your application can not share any data with another application. If you need two applications to use the same data, then that data can't be local to either of them.  Instead, you can use a web service and store the data on the web. Once your data is stored in the cloud, it can be accessed by as many applications you want.

  • 2. When an application is updated in Windows Phone Marketplace, its isolated storage folder is not modified. So if you change the data in Isolated Storage when upgrading your app to a new version, it is up to you to upgrade and modify your files so that the new version is working properly.

  • 3. IsolatedStorageSettings is not thread-safe and can throw IsolatedStorageException if used simultaneously from multiple threads.

  • 4. When a WP7 application is uninstalled, its isolated storage data will be deleted.

  • 5. Stored resources on a phone are limited.

  • 6. When a Windows Phone has only 10 percent of its storage space remaining, the user receives a status notification.

Best Practices


  • If using Directories always check if the Directory exists otherwise an exception could happen.

  • A directory must be empty before it is deleted. The deleted directory cannot be recovered once deleted.

  • It is a common practice to use  try{}catch{} when working with the Isolated Storage to prevent any unexpected exception to be seen by the user.

  • When working with files always use Using statement because it provides a convenient syntax that ensures the correct use of IDisposable objects.

  • When working with files always check if the Directory in which you want to create/delete a File exists.

  • Check for existing file before trying to read it

  • All Key/Value pairs in the IsolatedStorageSettings must be unique pairs, so before saving any value to the settings make sure that  it has a unique key.

  • Make sure that you have cleared all temporary data in Isolated Storage in case this data will no longer be used(usually upon application exit). A good practice is to create a temporary cache folder  that you can clear.

  • Make sure that users are enabled to delete any new data they have created. For example if users can generate files using your app they have to be able to delete these files as well.

  • Do not save important values like passwords as a plain text. Always try to encrypt them.


Source : http://www.windowsphonegeek.com & www.kunal-chowdhury.com

Hope, this will help you to understand the basics of Windows Phone 7 Programming!

Share

Post a Comment