COMP 446 / ELEC 446 Mobile Device Applications Scott Cutler Professor in the Practice of Computer Technology Department of Computer Science Department of Electrical and Computer Engineering cutler@rice.edu 10/10/12 Wednesday 2-3:30, DH 1046 Agenda Events of the Week Next week’s assignment and class Database Technology Questions on Assignment #5 SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 2 Events of the Week Official Apple Tribute to Steve Jobs video 10-05-2012 iPad Mini Slated for Oct. 17? Apple reportedly orders 10M iPad Minis for fourth quarter Google Maps to insert Street View into mobile Web app Google Motorola Drops Apple Lawsuit Microsoft Developing Windows Phone 8 Smartphone Samsung Posts $7.3 Billion Third-Quarter Profit, This Quarter Will Be Tougher HP CEO's Turnaround Message Flops On Wall Street T-Mobile, MetroPCS Deal Leaves Sprint Scrambling SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 3 Current Roster Victor Acuna Ryan Artecona Gbenga Badipe Peter Chang Joan Chao SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 4 Current Roster (2) Alex Chiu Heaven Chen Lingo Dai Weibo He Sahil Hingorani SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 5 Current Roster (3) Abdul Nimeri Bill Robertson Frank Salinas Tyler Siegert Austin Witt Matthew Zhao SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 6 COMP 446 Syllabus (updated) Lecture 1 - 8/22/12 - Introduction Lecture 2 - 8/29/12 - Assignment 1 issues and questions from videos Watch before: 3, 4 (demo part) Assignment: #3 – Graphing Universal Calculator Assignment due: Tuesday, 9/18 6:00 am (Monday 30:00) Lecture 4 - 9/12/12 - Assignment 3 issues and questions from videos Watch before: 1, 2 Assignment: #2 – Function Calculator Assignment due: Friday, 9/7 6:00 am (Thursday 30:00) Lecture 3 - 9/05/12 - Assignment 2 issues and questions from videos Watch before: None Assignment: #1 - Calculator Assignment due: Friday, 8/31 6:00 am (Thursday 30:00) Watch before: 4 (post demo), 5, 6 Assignment: Continue with #3 – Graphing Universal Calculator Assignment due: Tuesday, 9/18 6:00 am (Monday 30:00) Lecture 5 - 9/19/12 - Assignment 3 issues and questions from videos Watch before: Watch 7 before class, 8 after class Assignment: #4 – Flickr Top Places Assignment due: Friday, 10/05 6:00 am (Thursday 30:00) SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 7 COMP 446 Syllabus (updated) Lecture 6 - 9/26/12 - Assignment 4 technology preview Watch before: Assignment: 8, 9 very beginning of 10 1: #6 – Flickr Core Data - To be broken into required and optional parts - Optional part required for A+ 2: Final Project Topic (due before next class) Assignment due: Friday, 10/19 6:00 am (Thursday 30:00) Lecture 8 - 10/10/12 - Assignment 6 technology preview and SQL 8, 9 very beginning of 10 (for Assignment 4) #5 – Flickr Map Places Tasks 3, 4 and 8 are optional for extra credit (required for A+) Assignment due: Thursday, 10/11 6:00 am (Wednesday 30:00) (10/12 OK) Lecture 7 - 10/03/12 - Assignment 5 technology preview Watch by 10/3: Assignment: Watch for #5: 10 (after Tab Bar), 11, 12 Assignment: Final Project Proposal Assignment due: Tuesday, 10/23 6:00 am (Monday 30:00) Lecture 9 - 10/17/12 - iOS 6, Server technology Watch for #6: 13, 14 Assignment: iOS 6 app Assignment due: Friday, 10/26 6:00 am (Thursday 30:00) SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 8 COMP 446 Syllabus (updated) Lecture 10 - 10/24/12 - HTML 5, Windows Phone Lecture 11 - 10/31/12 – Android Assignment: Continue working on Final Project Assignment due: Wednesday, 11/28 2:00pm Lecture 14 - 11/21/12 – No class, but continue on final project Assignment: Continue working on Final Project Assignment due: Wednesday, 11/28 2:00pm Lecture 13 - 11/14/12 - Individual meetings on Final Project Watch before: Optional 17, 18 Assignment: Continue working on Final Project Assignment due: Wednesday, 11/28 2:00pm Lecture 12 - 11/7/12- Android vs. iOS vs. Windows Phone group debate Watch before: Optional 15, 16 Assignment: Major Final Project Feature Assignment due: Friday, 11/2 6:00 am (Thursday 30:00) Assignment: Continue working on Final Project Assignment due: Wednesday, 11/28 2:00pm Lecture 15 - 11/28/12 - Final Presentations Assignment: Document Final Project Assignment due: By time assigned for COMP 446 final (there is NO final exam) SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 9 Final Project Timeline New 9/19 9/26 10/03 10/10 10/17 10/24 10/31 11/7 11/14 11/21 11/28 12/5 - #4 10/05 - #5 10/12 - FP Topic 10/10 - #6 10/17 - FP Proposal 10/23 - Major Feature 11/2 - Individual Meetings 11/14 - Final Presentation 11/28 - Documentation due Finals Day SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 10 Deploying to Device Read deployment information at developer.apple.com 4 Steps Load intermediate certificate Request development certificate Send me device id to register Use Xcode to deploy. It will use team provisioning profile If you have problems, we will have a deployment session after class. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 11 Some iOS Devices Available iPhone iPhone 3G iPhone 3GS AT&T iPhone 4 Verizon 4S may be available SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 12 Week 9 Watch CS193p lectures #13 and #14 Make progress on CS193p Assignment #6 Try and complete before 10/17 class. Due Friday, 10/19 6:00 am Next week’s lecture – iOS 6 and Server Technology SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 13 What is coming up in the assignments? Assignment #5 – Fast Flickr Map Multi Threading / Grand Central Dispatch MapKit Deployment and cache aspects optional (Tasks 3, 4 and 8) Submissions which do not properly size initial view of images in both landscape and portrait on the iPad will be rejected. Assignment #6 – Core Data Flickr UIDocument Core Data Aspects outside of core data version of #5 optional You are on your own for assignment #6 SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 14 Image Scaling Large number of students still not properly sizing images. I am testing in both portrait and landscape. Let bounds auto scale to frame of parent, but check bounds as they may change with rotation Aspect ratio must be maintained Check aspect ratios to see which dimension dominates scaling. Scale up or down to make critical dimension just fit Make sure to set your scale factor for smooth changes. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 15 SQL 101 SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 16 SQL Documentation SQL Server 2008 Main Page http://www.microsoft.com/sqlserver/2008/en/us/default.aspx Online Documentation (launch point for all versions) http://technet.microsoft.com/en-us/library/bb545450.aspx Microsoft SQL Server 2008 Books Online (October 2009) Download http://www.microsoft.com/downloads/en/details.aspx?FamilyId= 765433F7-0983-4D7A-B628-0A98145BCB97&displaylang=en Online Root http://technet.microsoft.com/en-us/library/bb545450.aspx Transact-SQL Reference http://technet.microsoft.com/en-us/library/bb510741.aspx SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 17 SQL Language Elements (from Wikipedia article) Clauses, which are in some cases optional, constituent components of statements and queries. Expressions which can produce either scalar values or tables consisting of columns and rows of data. Predicates which specify conditions that can be evaluated to SQL threevalued logic (3VL) Boolean truth values and which are used to limit the effects of statements and queries, or to change program flow. Queries which retrieve data based on specific criteria. Statements which may have a persistent effect on schemas and data, or which may control transactions, program flow, connections, sessions, or diagnostics. SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar. Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 18 Queries (credit Wikipedia) A query includes a list of columns to be included in the final result immediately following the SELECT keyword. An asterisk ("*") can also be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause which indicates the table(s) from which data is to be retrieved. The FROM clause can include optional JOIN subclauses to specify the rules for joining (relating) tables. The WHERE clause includes a comparison predicate, which restricts the rows returned by the query. The WHERE clause eliminates all rows from the result set for which the comparison predicate does not evaluate to True. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 19 Queries (credit Wikipedia) The GROUP BY clause is used to project rows having common values into a smaller set of rows. GROUP BY is often used in conjunction with SQL aggregation functions or to eliminate duplicate rows from a result set. The WHERE clause is applied before the GROUP BY clause. The HAVING clause includes a predicate used to filter rows resulting from the GROUP BY clause. Because it acts on the results of the GROUP BY clause, aggregation functions can be used in the HAVING clause predicate. The ORDER BY clause identifies which columns are used to sort the resulting data, and in which direction they should be sorted (options are ascending or descending). Without an ORDER BY clause, the order of rows returned by an SQL query is undefined. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 20 Transact SQL (MSFT) Select Statement Retrieves rows from the database and enables the selection of one or many rows or columns from one or many tables. The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: [ WITH <common_table_expression>] SELECT select_list [ INTO new_table ] [ FROM table_source ] [ WHERE search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ] ] The UNION, EXCEPT and INTERSECT operators can be used between queries to combine or compare their results into one result set. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 21 Examples SELECT * FROM SELECT Name, Production.Product ORDER BY Name; ProductNumber, ListPrice AS Price FROM Production.Product ORDER BY Name; SELECT p.* SELECT Name, FROM Production.Product ProductNumber, ListPrice AS Price FROM Production.Product WHERE ProductLine = 'R' AND DaysToManufacture < 4 ORDER BY Name; AS p ORDER BY Name; SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 22 Examples SELECT p.Name AS ProductName, NonDiscountSales = (OrderQty * UnitPrice), Discounts = ((OrderQty * UnitPrice) * UnitPriceDiscount) FROM Production.Product AS p INNER JOIN Sales.SalesOrderDetail AS sod ON p.ProductID = sod.ProductID ORDER BY ProductName DESC; SELECT 'Total income is', ((OrderQty * UnitPrice) * (1.0 - UnitPriceDiscount)), ' for ', p.Name AS ProductName FROM Production.Product AS p INNER JOIN Sales.SalesOrderDetail AS sod ON p.ProductID = sod.ProductID ORDER BY ProductName ASC; SEC - 10/10/12 SELECT ProductID, SpecialOfferID, AVG(UnitPrice) AS 'Average Price', SUM(LineTotal) AS SubTotal FROM Sales.SalesOrderDetail GROUP BY ProductID, SpecialOfferID ORDER BY ProductID SELECT ProductModelID, AVG(ListPrice) AS 'Average List Price' FROM Production.Product WHERE ListPrice > $1000 GROUP BY ProductModelID ORDER BY ProductModelID; COMP 446 / ELEC 446 - Week 8 23 Examples SELECT ProductID, AVG(UnitPrice) AS 'Average Price' FROM Sales.SalesOrderDetail WHERE OrderQty > 10 GROUP BY ProductID ORDER BY AVG(UnitPrice); SELECT ProductID FROM Sales.SalesOrderDetail GROUP BY ProductID HAVING AVG(OrderQty) > 5 ORDER BY ProductID; SEC - 10/10/12 SELECT SalesOrderID, CarrierTrackingNumber FROM Sales.SalesOrderDetail GROUP BY SalesOrderID, CarrierTrackingNumber HAVING CarrierTrackingNumber LIKE '4BD%' ORDER BY SalesOrderID ; SELECT ProductID FROM Sales.SalesOrderDetail WHERE UnitPrice < 25.00 GROUP BY ProductID HAVING AVG(OrderQty) > 5 ORDER BY ProductID; COMP 446 / ELEC 446 - Week 8 24 Examples SELECT ProductID, AVG(OrderQty) AS AverageQuantity, SUM(LineTotal) AS Total FROM Sales.SalesOrderDetail GROUP BY ProductID HAVING SUM(LineTotal) > $1000000.00 AND AVG(OrderQty) < 3; SELECT ProductID, Total = SUM(LineTotal) FROM Sales.SalesOrderDetail GROUP BY ProductID HAVING SUM(LineTotal) > $2000000.00; SEC - 10/10/12 SELECT ProductID, SUM(LineTotal) AS Total FROM Sales.SalesOrderDetail GROUP BY ProductID HAVING COUNT(*) > 1500; SELECT DISTINCT c.LastName, c.FirstName FROM Person.Contact AS c JOIN HumanResources.Employee AS e ON e.ContactID = c.ContactID WHERE 5000.00 IN (SELECT Bonus FROM Sales.SalesPerson AS sp WHERE e.EmployeeID = sp.SalesPersonID); COMP 446 / ELEC 446 - Week 8 25 SQL Injection Attacks Easy and tempting to generate SQL queries in websites from entered values With nameTextbox, SQL = “select * from people where name =” + nameTextbox.text Rogue web bots respond with text that takes over server Adds closing tags followed by dangerous SQL commands Important to use Query Parameters instead or tightly check input fields. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 26 Preview of Videos 13 and 14 Many of the slides to follow come directly from the Fall 2011 Stanford CS193p slide deck. Some come from earlier versions of CS193p which I think were a bit clearer than the current deck SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 27 Core Data An Object-oriented approach to database technology Powerful, but exposes enough to make it complex How does it work? Create a visual mapping (using Xcode tool) between database and objects. Create and query for objects using object-oriented API. Access the “columns in the database table” using @propertys on those objects. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 28 Apple Speak for Core Data Tables are Entities Columns or fields are Attributes Joins are relationships Sort via array of sort descriptors Where clause via predicates SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 29 The Object-Oriented Structure Greatly simplified in iOS 5.0 Objects managed via NSManagedObjectContext Total database including NSManagedObjectContext managed by UIManagedDocument SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 30 UIManagedDocument Creates of opens database Asynchronous actions – completion handlers to know when complete Uses NSNotificationCenter to keep asynchronous operations coordinated Used to keep TableView cells automatically updated. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 31 NSManagedObject Subclasses Entities (tables) are managed objects or sets of managed objects Class definitions created by Xcode Need to be regenerated often Use “categories” to extend these classes SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 32 Categories Obective-C methods resolved at runtime Categories allow you to add a method understood by a class independent of the class Can reference class methods and iVars, but can not add new iVars Can add new methods and computed properties Most common usage is to create entity values SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 33 NSFetchRequest Objective-C means of doing a Query Elements of an NSFetchRequest Entity to fetch NSPredicate (optional filtering) NSSortDescriptor (ordered NSArray of results order) Fetch count (although objects are faulted in when needed) NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@“Photo”]; request.fetchBatchSize = 20; request.fetchLimit = 100; request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; request.predicate = ...; Returns NSArray of NSManagedObjects SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 34 NSPredicate NSString *serverName = @“flickr-5”; NSPredicate *predicate = [NSPredicate predicateWithFormat:@“thumbnailURL contains %@”, serverName]; Examples @“uniqueId = %@”, [flickrInfo objectForKey:@“id”] // unique a photo in the database @“name contains[c] %@”, (NSString *) // matches name case insensitively @“viewed > %@”, (NSDate *) // viewed is a Date attribute in the data mapping @“whoTook.name = %@”, (NSString *) // Photo search (by photographer’s name) @“any photos.title contains %@”, (NSString *) // Photographer search (not a Photo search) Many more options. Look at the class documentation for NSPredicate. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 35 Core Data Thread Safety NOT Thread Safe. Must run in thread that created NSManagedObjectContext [context performBlock:^{ … }]; SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 36 Core Data and Table View NSFetchedResultsController hooks an NSFetchRequest up to a UITableViewController Answers numberOfSectionsInTableView and numberOfRowsInSection Helps with table cell values via objectAtIndexPath Watches changes in Core Data and auto-updates table view SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 37 CoreDataTableViewController Documented in NSFetchedResultsController Combines UITableVIewCOntroller with NSFetchedResultsConrtoller and wires them together. Provided for you to use in assignment #6 SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 38 Video 14 Major demo getting photos by photographer using core data Contains all of the technologies needed to convert Assignment #5 to #6. SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 39 Assignment #5 Questions SEC - 10/10/12 COMP 446 / ELEC 446 - Week 8 40