ITP 342 Mobile App Development. Data Persistence

Similar documents
ITP 342 Mobile App Development. Data Persistence

App SandBox Directory

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

Review. Objective-C Classes, Methods, Properties, Protocols, Delegation, Memory Management

CSCI 251: iphone Application Development

Files & Archiving. Lecture 8

NSObject. - (NSString *)description Provides us with a string description of the object

ITP 342 Advanced Mobile App Dev. Core Data

Bindings Example Exercise James Dempsey - WWDC Pre-Show Cocoa Workshop

File System Programming Guide

Data Management: Data Types & Collections

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

CS193E Lecture 7. Document-based Applications NSTableView Key-Value Coding

Developing Applications for ios

Getting Help. iphone Application Programming Lecture 3: Foundation Classes. Data Structures in Objective C. Online Documentation.

iphone Application Programming Lecture 3: Foundation Classes

Mobile Application Development

Data IAP 2010 iphonedev.csail.mit.edu edward benson / Thursday, January 14, 2010

Contents. iphone Training. Industry Trainers. Classroom Training Online Training ON-DEMAND Training. Read what you need

COMP327 Mobile Computing Session: Lecture Set 4 - Data Persistence, Core Data and Concurrency

Assignment II: Foundation Calculator

Data Storage. Dr. Sarah Abraham

CNIT 121: Computer Forensics. 13 Investigating Mac OS X Systems

COCOA WORKSHOP PART 1. Andreas Monitzer

Mobile Application Programming. Data and Persistence

Porting Objective-C to Swift. Richard Ekle

ITP 342 Mobile App Dev. Fundamentals

Announcements. Lab 3 is due on Wednesday by 11:59 PM

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Spring Stanford CS193p Spring 2011

Advanced ios. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 20 11/01/2012

Data Management

ITP 342 Advanced Mobile App Dev. Memory

IBM Case Manager Mobile Version SDK for ios Developers' Guide IBM SC

Mobile Application Programming. Data and Persistence

Mobile Application Development L12: Storage & Communication

Collections & Memory Management. Lecture 2

Review (Basic Objective-C)

CS193p Spring 2010 Thursday, April 29, 2010

Introductory ios Development

Monday, 1 November The ios System

Developing Applications for ios

CS193P: HelloPoly Walkthrough

Developing Applications for ios

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

Finding Bugs Using Xcode Runtime Tools

An ios Static Library for Service Discovery and Dynamic Procedure Calls

Advanced Memory Analysis with Instruments. Daniel Delwood Performance Tools Engineer

CS 371L - Mobile Computing (ios) Dr. William C. Bulko. CS 371L Mobile Computing (ios) Introduction

Abstract Data Types. 2501ICT/7421ICTNathan. René Hexel. Semester 1, School of Information and Communication Technology Griffith University

ITP 342 Mobile App Dev. Fundamentals

ios: Objective-C Primer

Functional Reactive Programming on ios

About MSDOSX. Lecture 0

iphone Programming Patrick H. Madden SUNY Binghamton Computer Science Department

Document-Based App Programming Guide for Mac

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

Xcode 6 and ios 8 What s New for Software Developers

Chapter 6 Storage Management File-System Interface 11.1

DL/ID Parsing Component for ios

ios Development Lecture 1 Introduction to Objective-C Ing. Simone Cirani

COMP327 Mobile Computing Session:

ITP 342 Mobile App Dev. Unit Testing

Using Doxygen to Create Xcode Documentation Sets

Announcement. Final Project Proposal Presentations and Updates

CS193P - Lecture 16. iphone Application Development. Audio APIs Video Playback Displaying Web Content Settings

Principles of Programming Languages. Objective-C. Joris Kluivers

Swift. Introducing swift. Thomas Woodfin

PhoneView Product Manual

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

Getting Started with Command Prompts

Announcements. Today s Topics

iphone Development Setup Instructions Nikhil Yadav Pervasive Health Fall 2011

Life Cycle. Chapter Explore the Game Application. Understanding the Views in a Game

Desktop & Laptop Edition

Manual File System Check Mac Os X 10.8

E Event-based parser, XML, 180 Extended attributes, URLs, 118 API, 119 command line, 118 description, 118 NSURL category, 119

Objective-C and COCOA Applications

CS193E Lecture #3 Categories and Protocols Cocoa Memory Management

Getting Help. iphone Application Programming Lecture 3: Foundation Classes. Data Structures in Objective C. Online Documentation.

AshMUG P. DeGroot, Y. Fried 9/14/10! P. 1 of 10. BACKUPS Why? Where? How?

1 Download the latest version of ImageJ for your platform from the website:

Data storage and exchange in Android

AppleScript Overview

When we re first learning Cocoa (or Java, or Qt, or any other application framework),

A product of Byte Works, Inc. Credits Programming Mike Westerfield. Art Karen Bennett. Documentation Mike Westerfield

Author Bob Ippolito Conference. PyObjC Hacking. PyCon DC, March 2005

Praktikum Entwicklung von Mediensystemen mit ios

Xcode Release Notes. Apple offers a number of resources where you can get Xcode development support:

ITP 342 Mobile App Dev. Data Types

The Power of Predicates (Yes == NO) == NO

Xcode. Chapter 1. Creating a Project

Rename. Introduction. Rename 1

ITP 342 Mobile App Development. Singleton

Objective-C. Deck.m. Deck.h. Let s look at another class. This one represents a deck of cards. #import <Foundation/Foundation.h> #import "Deck.

Data Management CS 4720 Mobile Application Development

Xcode Release Notes. Apple offers a number of resources where you can get Xcode development support:

BackupVault Desktop & Laptop Edition. USER MANUAL For Microsoft Windows

Object-Oriented Programming in Objective-C

Design Error Code 3194 Iphone 4 Fix Windows 8

Using Virtual EEPROM and Flash API for Renesas MCUs RX600 Series

Transcription:

ITP 342 Mobile App Development Data Persistence

Persistent Storage Want our app to save its data to persistent storage Any form of nonvolatile storage that survives a restart of the device Want a user to make changes & have the changes stored and there when the user launches the app again A number of different mechanisms to persist data to the ios s file system User Defaults Property lists Object archives (or archiving) SQLite3 (ios s embedded relational database) Core Data (Apple s provided persistence tool) 2

The App Sandbox For security reasons, ios places each app (including its preferences and data) in a sandbox at install time A sandbox is a set of fine-grained controls that limit the app s access to files, preferences, network resources, hardware, and so on As part of the sandboxing process, the system installs each app in its own sandbox directory, which acts as the home for the app and its data 3

Sandbox Directories in ios Subdirectories Documents Library tmp 4

App Sandbox in Simulator (Xcode 5) Open a Finder window Press command-shift-g to use "Go to" Enter ~/Library/Application Support/iPhone Simulator Click on a version of the ios such as 7.0.3 Click on Applications You will see folders for Applications, Library, Media, Root, and tmp This listing represents the simulator, but the file structure is similar to what s on the actual device 5

Applications Folder The Applications folder is where ios stores its applications Drill down and you will see a bunch of folders with names that are long strings of characters These names are globally unique identifiers (GUIDs) and are generated automatically by Xcode Each folder represents an app Click on one of them (any of them) 6

Your App's Folder Drill down into one of the application subfolders Documents Your applications stores its data in Documents, with the exception of NSUserDefaults-based preference settings Library NSUserDefaults-based preference settings are stored in the Library/Preferences folder tmp Place to store temporary files that will not be backed up by itunes, but your app has to be responsibility for deleting the files 7

Settings App iphone and other ios devices have a dedicated application called Settings Goal: Add settings for your applications to the Settings application How to access those settings from your application 8

Settings Bundle A settings bundle is a groups of files built in to an application that tells the Settings application which preferences the application wishes to collect from the user The Settings app acts as a common user interface for the ios User Defaults mechanism User Defaults is the part of the system that stores and retrieves preferences In ios, User Defaults is implemented by the NSUserDefaults class It s the same class used to store and read preferences on the Mac Your apps will use NSUserDefaults to read and store preference data using a key value (like NSDictionary) NSUserDefaults data is persisted to the file system 9

NSUserDefaults Simple dictionary-like API Save basic data to disk NSString NSNumber NSDate NSArray NSDictionary (hashtables) 10

Save to user defaults Saving Answers 11

Consts const is a pointer to the object in memory It is more efficient than #define! #define is a pre-processor macro! Why use constants at all? You could type a constant s value in each and every place it s being used, but there is the human factor. You could easily mistype the string and the compiler would not complain about your grammar. This way is more convenient since Xcode tries to do his best to autocomplete stuff for us and these constants are no exception. 12

Placement Consts Declare constants outside of @interface and @implementation! Naming conventions UpperCamelCase May start will a two-letter upper-case prefix NSString *const UDKeyAnswerArray = @"AnswerArray";! Older Objective-C code used a k prefix NSString *const kuserdefaultskeysecretanswer = @"SecretAnswer";! 13

Use CamelCase 14

Saving Answers When do we need to save to the User Defaults (i.e. call our new save method)? 1. Add a new fortune to the array of answers Update the insertanswer: atindex: method 2. Remove a fortune to the array of answers Update the removeansweratindex: method 3. Set a new secret answer (i.e. secret fortune) Override the setter method for the secretanswer property setsecretanswer:! 15

User Defaults Contents Open Finder and find file ~/Library/Application Support/iPhone Simulator/ 7.0/Applications/<appid>/Library/Preferences/ edu.usc.itp.<appname>.plist Stored as a property list (plist) 16

Restoring Answers Model still needs to read from user defaults User Defaults collections are always immutable We need to create our own mutable copy Override the get method for the answers property Do a lazy load Get it the first time it is needed 17

Restoring Answers 18

Restoring Secret Answer Override the get method for the secretanswer property 19

NSUserDefault Limitations No high performance Meant for small amounts of data No complex structure or relationship support Not searchable 20

Save Files in Documents Instead of using User Defaults, let's put data into the Documents folder of our app (Lab5) You can write/read various types of files Property Lists (plists) Standard way to store text and settings since the early days of Cocoa Data can be either in XML or binary format Text Files Archiving Objects Save data objects as binary data through a process of encoding/decoding objects The encoding operation supports scalar types and objects that support the NSCoding protocol 21

Getting the Documents Folder Since our app is in a folder with a seemingly random name, how do we retrieve the full path to the Documents folder so we can read and write files? Use the C function NSSearchPathForDirectoriesInDomain() It s a Foundation function, also used by Cocoa for Mac OS X 22

Documents Folder The contant NSDocumentDirectory says we are looking for the path to the Documents directory The constant NSUserDomainMask indicates that we want to restrict our search to our app s sandbox It gives us an array of matching paths, but we know we are only going to get one which is at index 0 23

Getting the tmp Folder Use the Foundation function called NSTemporaryDirectory()! 24

Single File Persistence Using a single file is the easiest approach With many apps, it is a perfectly acceptable one You start off by creating a root object, usually an NSArray or NSDictionary It could also be based on a custom class Whenever you need to save, your code rewrites the entire contents of the root object to a single file When your app launches, it reads the entire contents of that file into memory; when it quits, it writes out the entire contents The downside of using a single file is that you need to load of all of your app s data into memory And you must write all of it to the file system for event the smallest changes 25

Multiple File Persistence It allows the app to load only data that the user has requested (another form of lazy loading) When the user makes a change, only the files that changed need to be saved This method also gives you the opportunity to free up memory when you receive a lowmemory notification The downside is that it adds a fair amount of complexity 26

Using Property Lists Property lists are convenient and can be edited manually using Xcode or the Property List Editor app Both NSDictionary and NSArray instances can be written to and created from property lists, as long as the dictionary or array contains only specific serializable object Serialized object - one that has been converted into a stream of bytes so it can be stored in a file or transferred over a network 27

Property List Serialization Only certain objects can be placed into a collection class, such as an NSDictionary or NSArray, and then stored to a property list using the collection class s writetofile:atomically method The following Objective-C classes can: NSArray, NSMutableArray NSDictionary, NSMutableDictionary NSData, NSMutableData NSString, NSMutableString NSNumber NSDate 28

Write To File The atomically parameter tells the method to write the data to an auxiliary file, not to the specified location Once it has successfully written the file, it will then copy the auxiliary file to the location specified by the first parameter This is a safer way to write a file: If the app crashes during the save, the existing file (if there was one) will not be corrupted It adds a bit of overhead, but in most situations, it s worth the cost [myarray writetofile:filepath atomically:yes];! 29

Persistent Model for Quotes We could change the code in our QuotesModel to use a plist file Call it something like quotes.plist 30

Create constants Saving Quotes Not required Just easier to work with 31

Saving Quotes Create a property of type NSString for the file path The model has a property of type NSMutableArray to hold the quotes Use that property to save to a file 32

Saving Quotes Initialize filepath & quotes in the init method Do not use the lazy load methods for quotes 33

Saving Quotes 34

Saving Quotes When do we need to save to the plist file (i.e. call our new save method)? 1. Add a new quote Update the insertquote: atindex: method or the insertquote: author: atindex: method 2. Remove a quote Update the removequoteatindex: method 35

ios 8 Simulator in Xcode 6 The Documents directory for apps have changed locations. Have your app print out the directory in the console. // XYZAppDelegate.m!! - (BOOL)application:(UIApplication *)application! didfinishlaunchingwithoptions:(nsdictionary *)launchoptions! {! #if TARGET_IPHONE_SIMULATOR! NSLog(@"Documents Directory: %@", [[[NSFileManager defaultmanager]! URLsForDirectory:NSDocumentDirectory! indomains:nsuserdomainmask] lastobject]);! #endif!! return YES;! }! 36

Embedded SQL SQLite3 is very efficient at storing and retrieving large amounts of data It s also capable of doing complex aggregations of our data, with much faster results than you would get doing the same thing using objects SQLite3 uses the Structured Query Language (SQL) It s the standard for interacting with relational databases http://www.sqlite.org/cintro.html http://www.sqlite.org/lang.html 37

Creating or Opening the DB Use the sqlite3_open() function to open an existing database If none exists at the specified location, it will create a new one If result is equal to the constant SQLITE_OK, then the database was successfully opened The path to the database file must be passed in as a C string, not as an NSString SQLite3 was written in portable C, not Objective-C sqlite3 *database;! int result = sqlite3_open("/path/to/database/file", &database);! 38

Core Data Converts objects to and from a persistent format SQLite on ios Provides modeling tools to capture the classes Allows you to edit objects and persist later Makes Undo easy Keeps local changes local Will have a separate presentation on this 39