cse436 lecture11 10-12-15 - washington university in st. louis€¦ · extensible networking...
TRANSCRIPT
Extensible Networking Platform 1 1 - CSE 436 – Software Engineering Workshop
Announcements • Lab 4 is due on Wednesday
• Lab 5 is posted – Due November 4th – Do not procras5nate on this lab, it is at least twice as much work as the previous labs (and worth twice as many points)
• Wednesday is a “Lab Day” in Whitaker 316 – Work on Labs 4 and 5 – No 8:30 AM Session
• I am assuming some students will not aKend the lab day
Extensible Networking Platform 2 2 - CSE 436 – Software Engineering Workshop
Today’s Topics
• Property Lists
• iPhone’s File System
• Archiving Objects
• SQLite
• Web Services
Extensible Networking Platform 3 3 - CSE 436 – Software Engineering Workshop
Storage on the iPhone
Property Lists
Extensible Networking Platform 4 4 - CSE 436 – Software Engineering Workshop
Property Lists
• Convenient way to store a small amount of data – Arrays, dic5onaries, strings, numbers, dates, raw data
– Human-‐readable XML or binary format • NSUserDefaults class uses property lists under the hood
Extensible Networking Platform 5 5 - CSE 436 – Software Engineering Workshop
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
Extensible Networking Platform 6 6 - CSE 436 – Software Engineering Workshop
Reading & WriSng Property Lists
• NSArray and NSDicSonary convenience methods • Operate recursively
// WriSng -‐ (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag; -‐ (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag;
// Reading -‐ (id)initWithContentsOfFile:(NSString *)aPath; -‐ (id)initWithContentsOfURL:(NSURL *)aURL;
Extensible Networking Platform 7 7 - CSE 436 – Software Engineering Workshop
WriSng an Array to Disk
NSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60],nil]; [array writeToFile:@“MyArray.plist” atomically:YES];
Extensible Networking Platform 8 8 - CSE 436 – Software Engineering Workshop
WriSng a DicSonary to Disk NSDicSonary *dict = [NSDicSonary dicSonaryWithObjectsAndKeys: @“Name”, @“Evan”, @“Lecture”, [NSNumber numberWithInt:10], nil]; [dict writeToFile:@“MyDict.plist” atomically:YES];
Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop
NSPropertyListSerializaSon
• Allows finer-‐grained control – File format – More descrip5ve errors – Mutability
// Property list to NSData
+ (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format errorDescripSon:(NSString **)errorString;
// NSData to property list + (id)propertyListFromData:(NSData *)data
mutabilityOpSon:(NSPropertyListMutabilityOpSons)opt format:(NSPropertyListFormat *)format errorDescripSon:(NSString **)errorString;
Extensible Networking Platform 10 10 - CSE 436 – Software Engineering Workshop
More on Property Lists
“Property List Programming Guide for Cocoa”
hjp://developer.apple.com/documentaSon/Cocoa/ Conceptual/PropertyLists/
Extensible Networking Platform 11 11 - CSE 436 – Software Engineering Workshop
iPhone’s File System
Extensible Networking Platform 12 12 - CSE 436 – Software Engineering Workshop
Keeping ApplicaSons Separate
Extensible Networking Platform 13 13 - CSE 436 – Software Engineering Workshop
Why Keep ApplicaSons Separate?
• Security
• Privacy
• Cleanup amer deleSng an app
Extensible Networking Platform 14 14 - CSE 436 – Software Engineering Workshop
Home Directory Layout • Each app has its own set of directories
• <ApplicaSon Home> – MyApp.app
• MyApp • MainWindow.nib • SomeImage.png
– Documents – Library
• Caches • Preferences
• ApplicaSons only read and write within their home directory
• Backed up by iTunes during sync (mostly)
Extensible Networking Platform 15 15 - CSE 436 – Software Engineering Workshop
Demo
Extensible Networking Platform 16 16 - CSE 436 – Software Engineering Workshop
File Paths in Your ApplicaSon // Basic directories NSString *homePath = NSHomeDirectory(); NSString *tmpPath = NSTemporaryDirectory(); // Documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; // <ApplicaSon Home>/Documents/foo.plist NSString *fooPath = [documentsPath
stringByAppendingPathComponent:@“foo.plist”];
Extensible Networking Platform 17 17 - CSE 436 – Software Engineering Workshop
Including Writable Files with Your App
• Many applicaSons want to include some starter data
• But applicaSon 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
Extensible Networking Platform 18 18 - CSE 436 – Software Engineering Workshop
Archiving Objects
Extensible Networking Platform 19 19 - CSE 436 – Software Engineering Workshop
Archiving Objects
• Next logical step from property lists – Include arbitrary classes – Complex object graphs
• Used by Interface Builder for NIBs
Extensible Networking Platform 20 20 - CSE 436 – Software Engineering Workshop
Making Objects Archivable • Conform to the <NSCoding> protocol // Encode an object for an archive -‐ (void)encodeWithCoder:(NSCoder *)coder { [super encodeWithCoder:coder]; [coder encodeObject:name forKey:@“Name”]; [coder encodeInteger:numberOfSides forKey:@“Sides”]; } // Decode an object from an archive -‐ (id)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; name = [[coder decodeObjectForKey:@“Name”] retain]; numberOfSides = [coder decodeIntegerForKey:@“Side”]; }
Extensible Networking Platform 21 21 - CSE 436 – Software Engineering Workshop
Archiving & Unarchiving Object Graphs
• CreaSng an archive NSArray *polygons = ...; NSString *path = ...; BOOL result = [NSKeyedArchiver archiveRootObject:polygons toFile:path];
• Decoding an archive NSArray *polygons = nil; NSString *path = ...; polygons = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
Extensible Networking Platform 22 22 - CSE 436 – Software Engineering Workshop
More on Archiving Objects
“Archives and SerializaSons Programming Guide for Cocoa” hjp://developer.apple.com/documentaSon/Cocoa/
Conceptual/Archiving/
Extensible Networking Platform 23 23 - CSE 436 – Software Engineering Workshop
SQLite
Extensible Networking Platform 24 24 - CSE 436 – Software Engineering Workshop
SQLite
• Complete SQL database in an ordinary file
• Simple, compact, fast, reliable
• No server
• Great for embedded devices – Included on the iPhone plaZorm
Extensible Networking Platform 25 25 - CSE 436 – Software Engineering Workshop
When Not to Use SQLite
• MulS-‐gigabyte databases
• High concurrency (mulSple writers)
• Client-‐server applicaSons
• “Appropriate Uses for SQLite” hjp://www.sqlite.org/whentouse.html
Extensible Networking Platform 26 26 - CSE 436 – Software Engineering Workshop
More on SQLite
• “SQLite in 5 Minutes Or Less” – hKp://www.sqlite.org/quickstart.html
• “Intro to the SQLite C Interface” – hKp://www.sqlite.org/cintro.html
• Example code available – hKp://research.engineering.wustl.edu/~todd/cse436/examples/MySQLiteTableView.zip
Extensible Networking Platform 27 27 - CSE 436 – Software Engineering Workshop
SQLite Demo
Extensible Networking Platform 28 28 - CSE 436 – Software Engineering Workshop
Core Data
• Object-‐graph management and persistence framework – Makes it easy to save and load model objects
• Proper5es • Rela5onships
– Higher-‐level abstrac5on than SQLite or property lists
• Available on iPhone 3.0 and up – hKp://developer.apple.com/iphone/library/documenta5on/DataManagement/
Conceptual/iPhoneCoreData01/Introduc5on/Introduc5on.html
Extensible Networking Platform 29 29 - CSE 436 – Software Engineering Workshop
Web Services
Extensible Networking Platform 30 30 - CSE 436 – Software Engineering Workshop
Your ApplicaSon & The Cloud
• Store & access remote data
• May be under your control or someone else’s
• Many Web 2.0 apps/sites provide developer API
Extensible Networking Platform 31 31 - CSE 436 – Software Engineering Workshop
IntegraSng with Web Services
• Non-‐goal of this class: teach you all about web services – Plenty of tutorials accessible, search on Google
• Many are exposed with XML or JSON
• High level overview of parsing these types of data
Extensible Networking Platform 32 32 - CSE 436 – Software Engineering Workshop
XML
Extensible Networking Platform 33 33 - CSE 436 – Software Engineering Workshop
OpSons for Parsing XML
• libxml2 – Tree-‐based: easy to parse, en5re tree in memory – Event-‐driven: less memory, more complex to manage state
– Text reader: fast, easy to write, efficient
• NSXMLParser – Event-‐driven API: simpler but less powerful than libxml2
Extensible Networking Platform 34 34 - CSE 436 – Software Engineering Workshop
More on Parsing XML
• Brent Simmons, “libxml2 + xmlTextReader on Macs” hjp://inessenSal.com/?comments=1&posSd=3489
– Includes example of parsing TwiKer XML!
• Big Nerd Ranch, “Parsing XML in Cocoa” hjp://weblog.bignerdranch.com/?p=48
– Covers the basics of NSXMLReader
Extensible Networking Platform 35 35 - CSE 436 – Software Engineering Workshop
JSON
Extensible Networking Platform 36 36 - CSE 436 – Software Engineering Workshop
JavaScript Object NotaSon
• More lightweight than XML
• Looks a lot like a property list – Arrays, dic5onaries, strings, numbers
• Open source json-‐framework wrapper for ObjecSve-‐C
Extensible Networking Platform 37 37 - CSE 436 – Software Engineering Workshop
What does a JSON string look like?
{ “instructor” : “Todd Sproull”, “students” : 20, “itunes-‐u” : true, “midterm-‐exam” : null, “assignments” : [ “WhatATool”, “HelloPoly”]
}
Extensible Networking Platform 38 38 - CSE 436 – Software Engineering Workshop
Using json-‐framework
• Reading a JSON string into FoundaSon objects #import <JSON/JSON.h> // Get a JSON string from the cloud NSString *jsonString = ...; // Parsing will result in FoundaSon objects // Top level may be an NSDicSonary or an NSArray id object = [jsonString JSONValue];
Extensible Networking Platform 39 39 - CSE 436 – Software Engineering Workshop
Using json-‐framework
• WriSng a JSON string from FoundaSon objects // Create some data in your app NSDicSonary *dicSonary = ...; // Convert into a JSON string before sending to the cloud jsonString = [dicSonary JSONRepresentaSon];
Extensible Networking Platform 40 40 - CSE 436 – Software Engineering Workshop
JSON Demo
Extensible Networking Platform 41 41 - CSE 436 – Software Engineering Workshop
More on JSON
• “JSON Parser/Generator for ObjecSve-‐C” – hKp://code.google.com/p/json-‐framework/
• “Introducing JSON”
– hKp://www.json.org/
• Example code available – hKp://research.engineering.wustl.edu/~todd/cse436/examples/
Extensible Networking Platform 42 42 - CSE 436 – Software Engineering Workshop
Recap
• Property lists – Quick & easy, but limited
• Archived objects – More flexible, but require wri5ng a lot of code
• SQLite and Core Data – Elegant solu5on for many types of problems
• XML and JSON – Low-‐overhead op5ons for talking to “the cloud”
Extensible Networking Platform 43 43 - CSE 436 – Software Engineering Workshop
Parse Demo