Download presentation
Presentation is loading. Please wait.
1
EEC-492/693/793 iPhone Application Development
Lecture 13 Wenbing Zhao & Nigamanth Sridhar 11/15/2018 EEC492/693/793 - iPhone Application Development
2
Outline Basic Data Persistence Assignments: iPhone file system
Property list Archiving SQLite Assignments: Build the Persistence app 11/15/2018 11/15/2018 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development 2
3
EEC492/693/793 - iPhone Application Development
iPhone File System Sand box mechanism Every app gets its own home directory, and apps are only allowed to read and write from their own home directory Normally Documents and tmp sub-directories Better security and easier to cleanup after deleting an app 11/15/2018 EEC492/693/793 - iPhone Application Development
4
App Home Directory Layout
<Application Home> MyApp.app MyApp MainWindow.nib SomeImage.png Documents Library Caches Preferences tmp 11/15/2018 EEC492/693/793 - iPhone Application Development
5
File Paths in Your Application
// Basic directories NSString *homePath = NSHomeDirectory(); NSString *tmpPath = NSTemporaryDirectory(); // Documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; // <Application Home>/Documents/foo.plist NSString *fooPath = [documentsPath 11/15/2018 EEC492/693/793 - iPhone Application Development
6
Including Writable Files with Your App
Many applications want to include some starter data But application bundles are code signed You can’t modify the contents of your app bundle To include a writable data file with your app... Build it as part of your app bundle On first launch, copy it to your Documents directory 11/15/2018 EEC492/693/793 - iPhone Application Development
7
EEC492/693/793 - iPhone Application Development
Property Lists Convenient way to store a small amount of data NSArray or NSDictionary of NSString, NSMutableString, NSNumber, NSDate, NSData, NSMutableData NSUserDefaults class uses property lists under the hood 11/15/2018 EEC492/693/793 - iPhone Application Development
8
When Not to Use Property Lists
More than a few hundred KB of data Loading a property list is all-or-nothing Complex object graphs Custom object types Multiple writers (e.g. not thread-safe) 11/15/2018 EEC492/693/793 - iPhone Application Development
9
Reading & Writing Property Lists
NSArray and NSDictionary convenience methods Operate recursively // Writing - (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag; - (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag; // Reading - (id)initWithContentsOfFile:(NSString *)aPath; - (id)initWithContentsOfURL:(NSURL *)aURL; 11/15/2018 EEC492/693/793 - iPhone Application Development
10
Writing Array & Dictionary to Disk
NSArray *array = [NSArray [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60], nil]; [array atomically:YES]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber [dict atomically:YES]; 11/15/2018 EEC492/693/793 - iPhone Application Development
11
EEC492/693/793 - iPhone Application Development
Archiving Objects Next logical step from property lists Include arbitrary classes Complex object graphs Used by Interface Builder for NIBs 11/15/2018 EEC492/693/793 - iPhone Application Development
12
Making Objects Archivable
Conform to the <NSCoding> protocol // Encode an object for an archive - (void)encodeWithCoder:(NSCoder *)coder { [super encodeWithCoder:coder]; [coder encodeObject:name [coder encodeInteger:numberOfSides } // Decode an object from an archive - (id)initWithCoder:(NSCoder *)coder self = [super initWithCoder:coder]; // if subclassing NSObject directly, need to check on // if(self = [super init]) … name = [[coder retain]; numberOfSides = [coder 11/15/2018 EEC492/693/793 - iPhone Application Development
13
EEC492/693/793 - iPhone Application Development
To Enable Copying Conform to the <NSCopying> protocol - (id)copyWithZone:(NSZone *)zone { MyClass *copy = [[[self class] allocWithZone:zone] init]; copy.foo = [self.foo copyWithZone:zone]; copy.bar = [self.bar copyWithZone:zone]; copy.someInt = self.someInt; copy.someFloat = self.someFloat; return copy; } 11/15/2018 EEC492/693/793 - iPhone Application Development
14
Archiving a Data Object
NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:myObject … [archiver finishEncoding]; BOOL success = [data atomically:YES]; [archiver release]; [data release]; 11/15/2018 EEC492/693/793 - iPhone Application Development
15
Unarchiving a Data Object
NSData *data = [[NSData alloc] initWithContentsOfFile:path]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; self.object = [unarchiver … [unarchiver finishDecoding]; [unarchiver release]; [data release]; The object returned by decodeObjectForKey: is autoreleased 11/15/2018 EEC492/693/793 - iPhone Application Development
16
EEC492/693/793 - iPhone Application Development
SQLite Complete SQL database in an ordinary file Simple, compact, fast, reliable No server Free/Open Source Software Great for embedded devices Included on the iPhone platform 11/15/2018 EEC492/693/793 - iPhone Application Development
17
EEC492/693/793 - iPhone Application Development
When Not to Use SQLite Multi-gigabyte databases High concurrency (multiple writers) Client-server applications “Appropriate Uses for SQLite” 11/15/2018 EEC492/693/793 - iPhone Application Development
18
EEC492/693/793 - iPhone Application Development
SQLite C API Basics Open the database int sqlite3_open(const char *filename, sqlite3 **db); Execute a SQL statement int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void*,int,char**,char**), void *context, char **error); // Your callback (when query is returned) int callback(void *context, int count, char **values, char **columns); Close the database int sqlite3_close(sqlite3 *db); Book is wrong on sqlite3_exec is used to run any command against SQLite3 that doesn’t return data (i.e., it could only be used for updates, inserts, and deletes). Actually, we can do query, you must handle the returned data in the callback (one callback per row returned) 11/15/2018 EEC492/693/793 - iPhone Application Development
19
Retrieving Data from SQLite3
NSString *query ID, FIELD_DATA FROM FIELDS ORDER BY ROW”; sqlite3_stmt *statement; int result = (sqlite3_prepare_v2( database, [query UTF8String], -1, &statement, nil); while (sqlite3_step(statement) == SQLITE_ROW) { int rowNum = sqlite3_column_int(statement, 0); char *rowData = (char *)sqlite3_column_text(statement, 1); NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData]; // Do something with the data here [fieldValue release]; } sqlite3_finalize(statement); 11/15/2018 EEC492/693/793 - iPhone Application Development
20
EEC492/693/793 - iPhone Application Development
Bind Variables Used when inserting into a database Making sure strings don’t have invalid characters, etc. char *sql = “insert into foo values (?, ?);”; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(database, sql, -1, &stmt, nil) == SQLITE_OK) { sqlite3_bind_int(stmt, 1, 235); sqlite3_bind_text(stmt, 2, “Bar”, -1, NULL); } if (sqlite3_step(stmt) != SQLITE_DONE) should be real error checking!”); sqlite3_finalize(stmt); Sqlite3_bind_text, beyond the first 3 parameters: The first is the length of the data being passed in the third parameter. In the case of C strings, you can pass -1 instead of the string’s length, and the function will use the entire string. In all other cases, you have to tell it the length of the data being passed int. The final parameter is an optional function callback in case you need to do any memory cleanup after the statement is executed 11/15/2018 EEC492/693/793 - iPhone Application Development
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.