30
DATA MODEL Building our App

iOS Beginners Lesson 3

Embed Size (px)

DESCRIPTION

iOS Beginners Lesson 2

Citation preview

Page 1: iOS Beginners Lesson 3

DATA MODELBuilding our App

Page 2: iOS Beginners Lesson 3

OVERVIEW

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface

Page 3: iOS Beginners Lesson 3

LESSON 3: DATA MODEL

• Hour 1: Local Storage and Data Class

• Hour 2: Dictionary and User Defaults

• Hour 3: Save to File

Page 4: iOS Beginners Lesson 3

Local Storage and Data Class

Page 5: iOS Beginners Lesson 3

• 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?

Page 6: iOS Beginners Lesson 3

Local Storage?

Page 7: iOS Beginners Lesson 3

<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

Page 8: iOS Beginners Lesson 3

<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

Page 9: iOS Beginners Lesson 3

<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

Page 10: iOS Beginners Lesson 3

<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

Page 11: iOS Beginners Lesson 3

<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

Page 12: iOS Beginners Lesson 3

• 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?

Page 13: iOS Beginners Lesson 3

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?

Page 14: iOS Beginners Lesson 3

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?

Page 15: iOS Beginners Lesson 3

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?

Page 16: iOS Beginners Lesson 3

• 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?

Page 17: iOS Beginners Lesson 3

• 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

Page 18: iOS Beginners Lesson 3

Create our “Data” class for Notes

Page 19: iOS Beginners Lesson 3

Create our “Data” class for Notes

Page 20: iOS Beginners Lesson 3

Create our “Data” class for Notes

Page 21: iOS Beginners Lesson 3

Create our “Data” class for Notes

Page 22: iOS Beginners Lesson 3

Create our “Data” class for Notes

Page 23: iOS Beginners Lesson 3

Dictionary and User Defaults

Page 24: iOS Beginners Lesson 3

Our first method

Page 25: iOS Beginners Lesson 3

Our first method

Page 26: iOS Beginners Lesson 3

Other methods

Page 27: iOS Beginners Lesson 3

Save to File

Page 28: iOS Beginners Lesson 3

Save into Filesystem

Page 29: iOS Beginners Lesson 3

Retrieve from Filesystem

Page 30: iOS Beginners Lesson 3

WHAT’S NEXT

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface