iOS Data Access
Common Data Access Patterns for Xamarin.iOS
- PDF for offline use
- Sample Code:
- Related Recipes:
- Related Links:
Let us know how you feel about this
last updated: 2016-10
Most applications have some requirement to save data on the device locally. Unless the amount of data is trivially small, this usually requires a database and a data layer in the application to manage database access. iOS has the SQLite database engine “built in” and access to store and retrieve data is simplified by Xamarin’s platform. This document shows how to access an SQLite database.
Xamarin.iOS supports database access APIs such as:
- ADO.NET framework.
- SQLite-NET 3rd party library.
This guide provides a high-level overview of databases in general before describing how to set up ADO.NET and SQLite.NET to access SQLite databases in your Xamarin.iOS applications.
The majority of the code in this document is completely cross-platform and will run on iOS or Android without modification. There are two sample apps discussed:
- DataAccess_Basic – Simple data operations writes the results to a text display control;
- DataAccess_Advanced – Integrates data operations into a small working application that lists and edits a simple data structure.
Both sample solutions contain iOS and Android sample application projects.
For Xamarin.Forms applications, read working with databases which explains how to work with SQLite in a PCL library with Xamarin.Forms.
This chapter discussed data access in Xamarin.iOS using SQLite as the database engine. The database can be accessed “directly” using ADO.NET syntax or you can include the SQLite.NET ORM and perform data operations in C#.
We reviewed two samples: one that contains very simple data access code that outputs to a text field, and a simple application that includes create, read, update and delete functionality. We also discussed threading and how to seed your application with a pre-populated SQLite database.
For additional examples of cross-platform data access see our Tasky Pro case study.