XAMARIN: DATA STORAGE USING FILE SYSTEM

CoverPicture.PNG
Photo credits to HDLOST

In today’s article, I will discuss how to use the File System of Xamarin forms in data storage. File System storage is a great option for you if you want to store a more larger data in the file system, maybe in the form of XML or JSON file, if Preference storage is not enough for you and Database is an overkill for you.

I will be using Xamarin Forms PCL type project and will be targeting both the Android and UWP platforms. Our sample application will be basic Read/Write operations from a Json file as shown in Figure 1 and Figure 2.

XamarinAndroidSample.gif
Figure 1. Sample Xamarin Android Application

 

XamarinUwpSample.gif
Figure 2. Sample UWP Application

Like the strategy we used in Preference, keep in mind that the implementation of Reading/Writing in file storage is different per platform. Android and IOS may use standard .NET classes like File Operations or in my case, I used Text Writer and Reader and UWP uses only Windows Storage.

Let’s create a blank Xamarin Forms PCL type project and modify our MainPage.xaml file so that it will look like Figure 3. Then let’s also add some event handlers in its code behind.

MainPageXaml.png
Figure 3. Xaml contents of our Main Page

 

Next, we design our Interface to handle both the operations of File APIs for both Android and UWP. Instead of using the traditional File Operations, we will use the TextWriter and TextReader due to asynchronous operations that are required to be implemented by the Windows Storage in the UWP. Kindly see the interface definition in Figure 4.

IFileReadWrite.png
Figure 4. Interface for our Read/Write operations

We then add per platform the Implementations of our IFileReadWrite Interface:

Android:

AndroidFileReadWrite.png
Figure 5. Android Implementation for IFileReadWrite

Important notes on the Android Implementation:

  • IFileReadWrite implementation should be registered with the Dependency serviceas a metadata attribute above its namespace.
  • Android and IOS implementation may use the same TextWriter and TextReader classes in order to Read/Write to a file. The only difference is the path of the File to be processed.

UWP:

UWPFileReadWrite.png
Figure 6. UWP Implementation of IFileReadWrite

Important note on UWP Implementation:

  • Unlike the Android and iOS platforms, the implementation of IPreference in the App.xaml.cs should be registered in UWP as shown in Figure 7.
RegisterUWP
Figure 7. Registration of FileReadWrite implementation of UWP
  • UWP Implementation uses the Windows Storage class.

We need to create a wrapper class called FileService for wrapping the Read/Write functionalities per platform and also add some Json File Serialization/Deserialization using the Newtonsoft Json plugin. Kindly see figure 8.

Wrapper.jpg
Figure 8. FileService wrapper for Read/Write operations

Let’s create a static property in our MainPage.xaml.cs file which will be used to access our wrapper class FileService as shown in Figure 9.

StaticProperty.png
Figure 9. Static property for handling FileService.

We will start to code in our MainPage.xaml.cs for our Add and Refresh button events as shown in figure 10.

MainPageXamlcs.png
Figure 10. Code behind for Event handlers

Try to build and run our Android and UWP application. We expect that the output should be the same with Figure 1 and 2.

Source Code

https://github.com/KirkPatrickJunsay/XamarinFormsFileStorage

Conclusion

Just a quick wrap up for the things we’ve learned, if you require more storage or the business requirement needs to Read/Write in files then your preferred storage technique should be File System. Also remember that different platform implements different File APIs so we need to have an abstraction using the built in Xamarin Forms dependency service. In my next article, I will discuss the third strategy on data storage which is Database.

Happy Coding 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s