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
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 Long term Saved on
Updates
.app/
Documents
Library
Library/Preferences
Library/Caches tmp
X
X
X
(Use
NSDefaults)
X
X
X
X
X
X
X
X
X
X
Saved to iCloud by
Default
Visible to User in iTunes
X
X
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 var cache = Path.Combine (documents, "..", "Library", "Caches"); 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
ExecuteNonQuery
ExecuteReader
ExecuteScalar
QUERIES
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]
Insert
Get<T>
Table<T>
Delete
Query<T>
Execute
QUERIES
SQLITE SECURITY
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
Loren.horsager@mcomposer.com
@lorenhorsager