We now turn our attention to data storage requirements; we need a way to store and retrieve Point of Interest (POI) data. This chapter covers the following topics:
Data storage solutions fall into two general categories, web services and local storage. While many real-world mobile apps rely on web services, we will focus on a local storage solution for a couple of reasons. First, it eliminates the need to maintain a hosted service for the example to work, and second, we simply do not have sufficient time to adequately deal with creating and accessing a web service solution in this book.
There are a number of solutions that can be used for storing data locally. SQLite is a lightweight transactional database engine that is delivered with the Android platform. SQLite is widely used on mobile platforms such as Android and iOS as well as on embedded systems of all types. SQLite is a great solution if you need the robust capabilities that a relational engine provides, but we have much simpler requirements; a simple file-based solution will be adequate for POIApp
.
As such, we have decided to go with a solution that stores the POI data as local files. This leads us to our next decision: how do we structure the data inside the file? JSON and XML are the two predominant standards that are used to store structured data in a text format. XML tends to be more verbose and is used in conjunction with SOAP/WSDL based web services. Data encoded with JSON is smaller than the equivalent XML, and thus, JSON has gained a lot of traction in the RESTful web-service community. Either of these standards meet our needs. The example presented in this chapter uses a JSON-based solution. In order to accommodate other implementations in the future we will, however, establish a standard interface for the storage service. This means that other implementations such as a web-service version of the XML-based version could be developed and plugged in with a minimal effort.
The first class that is needed is the one that represents the primary focus of the application, a Point of Interest class. POIApp
will allow the following attributes to be captured for a Point of Interest:
Id
Name
Description
Address
Latitude
Longitude
The POI entity class can be nothing more than a simple .NET class, which houses these attributes.
To create a POI entity class, perform the following steps:
POIApp
project from the Solution pad in Xamarin Studio. Select the POIApp
project and not the solution which is the top-level node in the Solution pad.PointOfInterest
and click on OK. The class will be created in the POIApp
project folder.The following code snippet is from 9169_04_CodesPOIAppPOIAppPointOfInterest.cs
from the code bundle available for this book:
public class PointOfInterest { public int? Id { get; set;} public string Name { get; set; } public string Description { get; set; } public string Address { get; set; } public double? Latitude { get; set; } public double? Longitude { get; set; } }
Note that the Id
, Latitude
, and Longitude
attributes are all marked as nullable. In the case of Latitude
and Longitude
, (0,0) is actually a valid location so a null value indicates the attributes have never been set. The Id
attribute is required but having a null value tells POIJsonService
to assign an actual value when the POI is saved.
18.216.186.164