Xamarin Local Storage

Report
DATA STORAGE WITH
XAMARIN
May 27, 2014
Loren Horsager
CEO, Founder
LOREN HORSAGER
 20 years of
software
consulting
 Created about
50 mobile apps
 Founder of
Mobile
Composer
LOCAL STORAGE - USE CASES
User Preferences
Login Credentials
Developing API
Data Collection
Offline
LOCAL STORAGE
Key/Value pairs
Files
Text Files
Binary Data
Serialized Files
Database (SQLite)
KEY/VALUE PAIRS
Simple Data
User Settings
Personalization info
Login tokens
Supports syncing to iCloud
KEY/VALUE PAIR DEMO
SIMPLE STORAGE
Download from Component Store (free)
using PerpetualEngine.Storage;
Consistent across platforms (Android, iOS,
Win)
Registry like data storage
SIMPLE STORAGE DEMO
KEY/VALUE PAIRS
Not secure
Saved with App Updates
Stored in Library/Preferences
FILE STORAGE
Writeable
.app/
Long term
Saved on
Updates
Saved to
iCloud by
Default
Visible to User
in iTunes
X
X
Documents
X
X
X
X
Library
X
X
X
X
Library/Preferences
X
X
X
X
(Use
NSDefaults)
Library/Caches
X
tmp
X
X
SYSTEM.IO
 Standard .NET library
 File, Directory, FileInfo, Path
PATHS
Path
.app/
./
Documents
var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
Library
var library = Path.Combine (documents, "..", "Library");
Library/Preferences
Use NSDefaults
Library/Caches
var cache = Path.Combine (documents, "..", "Library", "Caches");
tmp
var tmp = Path.Combine (documents, "..", "tmp");
SHARING FILES VIA ITUNES
Documents directory will automatically be
shared
To Disable:
 Change UIFileSharingEnabled in Info.plist
Users can only see root directory
Users can change files in Documents directory
so make sure it doesn’t impact your app
ICLOUD BACKUP
Limit the amount of data going to iCloud
Users get 5GB for all apps
Limit to files that can’t be recreated
To turn off iCloud syncing
 SetSkipBackupAttribute
NSFileManager.SetSkipBackupAttribute (filename, true);
APP UPDATES
When updating app versions the contents of
the following will remain
 Documents
 Library
This includes sub folders except
Library/Caches
FILE IO DEMO
SERIALIZATION
XML or JSON
Great for developing frontend prior to building
web services
Great for preferences
XML SERIALIZATION DEMO
SQLITE
When you need
 Structured data
 Complex queries
 Sorting
 Aggregating
 Efficient data storage
 Offline mode
 Shared data models
SQLITE
Open Source
Lightweight
Cross Platform
 Android
 iOS
 Win
Single file storage
Most of SQL92 standard
SQLITE DRAWBACKS
Some OUTER JOINS not supported
Can’t change scheme
Exceptions: Table rename, AddColumn
Views are Read-only
SQLITE OPTIONS
ADO.NET
SQLite.NET ORM
THREADING
Can’t share data access across threads
Wrap all reads and writes into the same lock
object locker = new object(); // class level private field
// rest of class code
lock (locker){
// Do your query or insert here
}
SQLITE ADO DEMO
QUERIES
ExecuteNonQuery
ExecuteReader
ExecuteScalar
SQLITE.NET ORM
Use existing entities
Does not support entity relationships
Different annotations than Entity Framework
Data Annotations
Simplifies data access
SQLITE.NET ORM DEMO
SQLITE.NET ORM ATTRIBUTES
[PrimaryKey]
[AutoIncrement]
[Column(name)]
[Table(name)]
[MaxLength(value)]
[Ignore]
[Unique]
QUERIES
Insert
Get<T>
Table<T>
Delete
Query<T>
Execute
SQLITE SECURIT Y
 SQLite is file based and not encrypted
 For secure storage use SQLCipher
 Available on Xamarin Component Store
CONSIDERATIONS
Security
 Don’t store logins directly
 Put data where it can’t be directly viewed or altered
by user
 Secure SQLite
 SQLCipher – encrypted SQLite database files ($499 from
Component Store)
iCloud
 Don’t sync more than is needed
RESOURCES
 Xamarin site
 http://docs.xamarin.com/guides/ios/application_fundamentals/working_wit
h_the_file_system/
 http://docs.xamarin.com/guides/crossplatform/application_fundamentals/data/part_4_using_adonet /
 http://docs.xamarin.com/guides/crossplatform/application_fundamentals/data/part_3_using_sqlite_orm /
 SimpleStorage
 https://components.xamarin.com/gettingstarted/simple -storage/true
CONTACT
 GitHub project
https://github.com/lhorsager/XamarinLocalStorage
 Presentation
http://lorenhorsager.com/2014/05/27/xamarin -local-storagepresentation/
Loren Horsager
[email protected]
@lorenhorsager

similar documents