DATA MODEL
Building our App
OVERVIEW
• Lesson 1: Introductions
• Lesson 2: iOS specifics
• Lesson 3: Data Model
• Lesson 4: Logic (Controller) & Inter...
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...
Local Storage?
<Application Home>/AppName.app
• Known as the bundle directory
• Do not write anything into this directory
• To prevent ta...
<Application Home>/Documents/
• Store critical user documents and app data files
• Critical data refers to any data that c...
<Application Home>/Documents/Inbox/
• Use this directory to access files that your app was
asked to open by outside entiti...
<Application Home>/Library/
• Non-user data files. This directory should not be used for user data files. Use it for your ...
<Application Home>/tmp/
• This directory is used to place data which we
do not need to keep for extended periods of
time
•...
• User Defaults (the ‘standard’ plist)
• Different file types (PDFs, text, doc, images
as png, jpg etc etc)
• CoreData (SQ...
User Defaults, also known as “Shared
Preferences”
• Key/value pairs
• Managed through Objective-C
• Stored in XML files th...
File Storage
• Files can be created and read on disk
• No special file type, so store any file type that
you like (XML, JS...
SQLite and Core Data
• Structured data with relationships can be saved into
an SQLite database
• iOS provides built-in sup...
• We can use User Defaults
• Each note can be represented as a key-value pair in User
Defaults
‣ key: a unique date-time s...
• Representation of what your app does (using custom
classes, subclass from NSObject for example)
• Spaceship game app wil...
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) & In...
Upcoming SlideShare
Loading in …5
×

iOS Beginners Lesson 3

388 views

Published on

iOS Beginners Lesson 2

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
388
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

iOS Beginners Lesson 3

  1. 1. DATA MODEL Building our App
  2. 2. OVERVIEW • Lesson 1: Introductions • Lesson 2: iOS specifics • Lesson 3: Data Model • Lesson 4: Logic (Controller) & Interface
  3. 3. LESSON 3: DATA MODEL • Hour 1: Local Storage and Data Class • Hour 2: Dictionary and User Defaults • Hour 3: Save to File
  4. 4. Local Storage and Data Class
  5. 5. • 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 Local Storage?
  6. 6. Local Storage?
  7. 7. <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
  8. 8. <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
  9. 9. <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
  10. 10. <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
  11. 11. <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
  12. 12. • 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?
  13. 13. 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?
  14. 14. 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?
  15. 15. 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?
  16. 16. • 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?
  17. 17. • 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
  18. 18. Create our “Data” class for Notes
  19. 19. Create our “Data” class for Notes
  20. 20. Create our “Data” class for Notes
  21. 21. Create our “Data” class for Notes
  22. 22. Create our “Data” class for Notes
  23. 23. Dictionary and User Defaults
  24. 24. Our first method
  25. 25. Our first method
  26. 26. Other methods
  27. 27. Save to File
  28. 28. Save into Filesystem
  29. 29. Retrieve from Filesystem
  30. 30. WHAT’S NEXT • Lesson 1: Introductions • Lesson 2: iOS specifics • Lesson 3: Data Model • Lesson 4: Logic (Controller) & Interface

×