iOS Beginners Lesson 3

Preview:

DESCRIPTION

iOS Beginners Lesson 2

Citation preview

DATA MODELBuilding our App

OVERVIEW

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface

LESSON 3: DATA MODEL

• Hour 1: Local Storage and Data Class

• Hour 2: Dictionary and User Defaults

• Hour 3: Save to File

Local Storage and Data Class

• Instantiating objects from classes (NSObject) keep your object’s data in your app/device memory

• These data “disappears” when your app is shut down or if you shut down/reboot your phone

• To persist your data for your user, you need “local storage” (i.e. data is written into our device’s disk/file system)

Local Storage?

Local Storage?

<Application Home>/AppName.app

• Known as the bundle directory

• Do not write anything into this directory

• To prevent tampering, the bundle directory is signed at installation time

• If you write into this directory after your app launches, your app cannot be launched again

Commonly used directories for Storage

<Application Home>/Documents/

• Store critical user documents and app data files

• Critical data refers to any data that cannot be re-created by our app

• Contents of this directory can be made available to user through file sharing

• Contents of this directory are backed up by iTunes

Commonly used directories for Storage

<Application Home>/Documents/Inbox/

• Use this directory to access files that your app was asked to open by outside entities, e.g. Mail program

• Your app can read and delete files in this directory but cannot create new files or write into existing files

• If user needs to edit file in this directory, app must silently move it out of directory before making changes

• Contents of this directory are backed up by iTunes

Commonly used directories for Storage

<Application Home>/Library/

• Non-user data files. This directory should not be used for user data files. Use it for your app support files.

• The contents of this directory, except for the Caches subdirectory, are backed up by iTunes

• iOS 5.0 & earlier, put app support files in <Application Home>/Library/Caches so these files don’t get backed up

• iOS 5.0.1 and later, app support files go into <Application Home>/Library/Application Support directory and is applied with the com.apple.MobileBackup extended attribute to each support file so files do not get backed up to either iTunes or iCloud

• If you have a lot of support files, store them all in a custom sub-directory and apply the extended attribute to that sub-directory

• Data cache files are placed in <Application Home>/Library/Caches directory. Database cache files, downloadable content used by magazine, newspaper and map apps are also stored here. Your app needs to be able to gracefully handle situations where cached data is deleted by the system to free up disk space

Commonly used directories for Storage

<Application Home>/tmp/

• This directory is used to place data which we do not need to keep for extended periods of time

• It’s a good habit to delete files in this tmp directory when we no longer need to use the file we have placed there temporarily.

Commonly used directories for Storage

• User Defaults (the ‘standard’ plist)

• Different file types (PDFs, text, doc, images as png, jpg etc etc)

• CoreData (SQLite database)

• 3rd party solutions like Realm.io

Options for Local Storage?

User Defaults, also known as “Shared Preferences”

• Key/value pairs

• Managed through Objective-C

• Stored in XML files that are managed through Apple’s API

• Stored in unencrypted format (“plain text”)

Options for Local Storage?

File Storage

• Files can be created and read on disk

• No special file type, so store any file type that you like (XML, JSON, delimited data csv, PDF, png, jpg etc)

• Store these files “internal to app” or “external to app” (in a shared file locations, such a Photo Albums)

Options for Local Storage?

SQLite and Core Data

• Structured data with relationships can be saved into an SQLite database

• iOS provides built-in support for SQLite (www.sqlite.org)

• All classes and interfaces are in CoreData package

• Database files are stored in “internal location” in app (within the app sandbox)

Options for Local Storage?

• We can use User Defaults

• Each note can be represented as a key-value pair in User Defaults

‣ key: a unique date-time stamp

‣ value: string

• Practical example: User defaults (NSUserDefaults)

• Practical example: File Storage

How do we store Notes?

• Representation of what your app does (using custom classes, subclass from NSObject for example)

• Spaceship game app will have Spaceship class, TakeNotes app will have Note class, Painting app could define a Image class

• Can subclass from UIDocument as well if we want to define a document-based data model

• Other possibilities if using CoreData (NSManagedObject) or Realm.io (RLMObject)

Data model objects

Create our “Data” class for Notes

Create our “Data” class for Notes

Create our “Data” class for Notes

Create our “Data” class for Notes

Create our “Data” class for Notes

Dictionary and User Defaults

Our first method

Our first method

Other methods

Save to File

Save into Filesystem

Retrieve from Filesystem

WHAT’S NEXT

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface