370 likes | 457 Views
Wednesday 2-4, DH 1046. 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 9/14/11. Today’s Agenda. Events of the week
E N D
Wednesday 2-4, DH 1046 • 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 9/14/11
Today’s Agenda • Events of the week • Final Project Philosophy • Review of current assignment • Memory Management • Next Week • Views, drawing, application life cycle • Assignment 3, Videos 5 and 6 • Graphing version of this week’s calculator COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 2
Events of the Week • Google: Mobile devices to generate 15% of Black Friday searches • Google buys Zagat, in original-content gambit • Google to debut Dart, a new language for the Web • Best Buy Adds RIM's Playbook To Heap Of El Cheapo Non-iPads • Windows Phone Marketplace boots AVG app • Windows 8 unveiled • Nvidia launches Windows 8 developer program • Nvidia Readies Quad-Core Tegra Chips for Mobile Devices • Apple iPads, iPhones get Flash video at last (not really) • Flash-derived iPad game tops App Store charts • AMD's FX CPU hits record-breaking speeds • Microsoft to update the Xbox 360's user interface • HTC interested in buying its own mobile OS • How HTML5 may become the standard for apps • Intel and Google to partner on next-gen Android devices COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 3
Assignment Mulligan • Large percentage of assignments late. • Issues with every submission • Difficult but very fundamental topic • Time for your one and only Mulligan COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 4
Assignment Planning • Much of this material is confusing until understood. • Assignments meant to pace you through major elements • Weekly video 1 introduces concepts. Video 2 demonstrates concepts required by homework. • Very difficult to get homework done without watching videos. • Expect something not to work as expected! COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 5
Anticipating Issues • Actual coding of assignment may take less than an hour if you are an expert; however . . . • Understanding what is wanted and how to approach takes time • iOS can be very confusing at the beginning • Bugs often obscure, manifesting themselves in places no where near the problem • Can easily get stumped for hours • Starting the assignment near the end is highly risky. • You will not get another Mulligan COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 6
COMP 446 Syllabus • 8/24/11Introduction / What makes mobile apps special Videos: Lecture 1. Introduction to Cocoa Touch, Objective-C, Tools, and MVC (September 21, 2010) Lecture 2. Building a Simple Calculator (September 23, 2010) Assignment: #1 and #1 Walkthrough - Calculator Due: Preferably watch videos this week, definitely by 8/30. Assignment due 9/1/11. • 8/31/11Discussion of first applications, Development Environment Videos: Lecture 3. Objective-C and Foundation Frameworks (September 28, 2010) Lecture 4. Foundation and Memory Management (September 30, 2010) Assignment: #2 Function Calculator (Due 9/15/11) • 9/7/11 Intro to Cocoa Touch, Objective-C, Using Objective-C, Foundation framework Videos: Assignment: • 9/14/11Memory Management Videos: Lecture 5. Protocols and Views (October 5, 2010) Lecture 6. Application & View Controller Lifecycle, Navigation Controller (October 7, 2010) Assignment: #3 Graphing Calculator (Due 9/22/11) • 9/21/11iPhone (pseudo) Web Apps / ASP.Net / Browser Capabilities Videos: Lecture 7. More Controllers of Controllers, iPad, Universal Applications (October 12, 2010) Lecture 8. Gesture Recognizers (October 14, 2010) Assignment: #4 Universal Calculator (Due 9/29/11) COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 7
COMP 446 Syllabus • 9/28/11SQL 101 Videos: Lecture 9. Image View, Web View, and Scroll View (October 19, 2010) Lecture 10. Table View (October 21, 2010) Assignment: #5 Pictures Places (Due 10/13/11) • 10/5/11Creating and Consuming Web Services Videos: Lecture 11. Persistence (October 26, 2010) Lecture 12. Core Data and Table Views (October 28, 2010) Assignment: #6 Core Data Places (Due 10/20/11, but watch lectures prior to 10/12/11 class) • 10/12/11Final Project discussion and Pairing Videos: Debugging Tools (October 22, 2010) Kleiner Perkins iFund (October 15, 2010) Assignment: Final Project Proposal (Due 10/18/11 along with Core Data Places due 10/20/11) • 10/19/11iOS 5 Videos: Lecture 13. Blocks and Multithreading (November 2, 2010) Lecture 14. Core Location and Map Kit (November 9, 2010) Assignment: Final Project, Stage 1 (Due 10/27/11) • 10/26/11Android Videos: Lecture 15. Editable Text, Modal View Controllers, and View Animation (Nov 11, 2010) Lecture 16. Core Motion, Segmented Control, and Alerts (November 16, 2010) Assignment: Advanced Feature App (Due 11/03/11) COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 8
COMP 446 Syllabus • 11/2/11Windows Phone 7, Final Project Prep Videos: Lecture 17. Media (November 18, 2010) Lecture 18. Accessibility on iOS: Make an App for Everyone (November 30, 2010) Assignment: Final Project, Stage 2 (Due 11/10/2011) • 11/09/11Final Project Prep / iPhone v. Android v. Windows Phone 7 group discussion Videos: Lessons from Bezos, Pincus, Young: CEO 2.0 (November 12, 2010) (Optional) LinkedIn: Shipping with CoreData (November 5, 2010) (Optional) Assignment: Final Project, Stage 3 (Due 11/17/2011) • 11/16/11Final Project Prep Videos: Building Flipboard (November 19, 2010) (Optional) Assignment: Final Project Presentation (Due 11/30/2011) • 11/23/11Thanksgiving – Continue Final Projects on your own • 11/30/11Demonstrate Final Projects to class • 12/4/11Class Offsite – Sunday (tentative) • FinalsSubmission of final code due by official finals date COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 9
Final Project • To be determined by end of 6th week • Can target iOS, Android or WP7 device • Topic chosen class before mid semester break • Proposal submitted by 10/18/11 • Full Spec 10/27/11 • Various milestones including presentation to class on 12/30/11 • Final code for project incorporating comments from presentation due on the official finals date of the course. • This is VERY important: If you do not have a great project definition, the 2nd half of the course will suffer. With a great project, this will be an exciting course. • Projects can be done solo and in pairs. Richard Latimer can not be part of project team. • Great if your project could have life after the class finishes COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 10
Week 5 • Watch CS193p lectures #5 and #6 • Preferably before Saturday. Definitely before next class. • Complete CS193p Assignment #3 • Try and complete before next class. Due day after next class. • Submit assignments, review lectures at www.comp446.com • Start thinking about big project COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 11
Memory Management COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 12
Memory Management Basics • Object Creation • + alloc and - init methods • Objects take space, space allocated with alloc and initialized with init. • Objects kept alive via a retention count. • [object retain] increases that count by 1 • [object release] decrease that count by 1 • When count reaches 0, [object dealloc] automatically called. • Objects retained by other objects need to be released when finished • Either when explicitly finished or in the dealloc method • Objects which might get released to 0 by others need to be retained COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 13
Pointers vs. Objects • Objects exist in the heap and take up resources • Variable names, for example (NSString *) point at objects. Their value is a pointer to the actual object. • Names can be set to nil (0) when they point to nothing • C values (e.g double, int) are stored with class instance and do not have to be managed COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 14
Pointers vs. Objects • Copying objects, particularly NSString guarantees they won’t be changed from under you • You may cast them as an NSString, but it might be an NSMutableString • Multiple names can point to the same object • (NSMutableString *)mstring1 = [NSMutableString setString:@”Hi”];(NSString *)string2 = mstring1; // string2 == mstring1; both point to same object COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 15
Instance Variables and Properties Memory • Instance variables take up some space • int, float, etc store the actual values • (NSString *) iVar saves room for a pointer to an NSString object • The space needed by the actual object goes away when the object is deallocated, not when the pointer goes away. • Variables initialized to 0 or nil. This includes pointer objects. • Instance variables assigned to objects need to be released during [object dealloc] • OK to message nil objects, notOK to release deallocated objects COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 16
Example: NSString Setter (retain) • - (void)setName:(NSString *)newName { if (name != newName) { [name release]; //old name retain count down 1 name = [newName retain]; // name’s retain count has been bumped up by 1} COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 17
Example: NSString Setter (copy) • - (void)setName:(NSString *)newName { if (name != newName) { [name release]; //old name retain count down 1 name = [newName copy]; // name has retain count of 1, we own it} COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 18
Which @property attribute to use • @property (retain) id expression; • Use retain for most objects. • Setter will properly release previous value and retain new value • @property (copy) NSString *aString; • Use for any object where the underlying object might change. • Often done for NSString as it might be owned by someone else as an NSMutableString. • You may want to use it for NSArray, NSDictionary, NSSet • @property (assign) UITableViewDelegate *myDelegate; • Primarily used for objects which don’t go away; specifically delegates. COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 19
Property Memory Management • Header options • readonly – getter, no setter. Property can not be changed. • readwrite (default) – setter and getter • assign, retain and copy relate to how setter is coded assign retain copy • (void)setName: (NSString *)value • { • if (value != name) { • // [name release]; • name = value; • } • } // Probably you would not want this • (void)setName: (NSString *)value • { • if (value != name) { • [name release]; • name = [value retain]; • } • } //Name points to other object • (void)setName: (NSString *)value • { • if (value != name) { • [name release]; • name = [value copy]; • } • } // Creates a copy which you own COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11
Autorelease • If you know you are finished with an object and you retained or own it, release it when finished. It will go away if you are the last reference. • If you no longer need the object yourself, but want to pass it to someone else, autorelease the object; let them be responsible for retaining it. • If you know you will be done with the object during this run loop but can’t tell where in your code you may finish with it, autorelease the object. • If you acquire an autoreleased object and believe you will need it on a different run-loop cycle, retain the object and later release it. • Do not sprinkle autoreleases. They should be deliberate. COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 21
Initial Retention Count • Objects received through [[class alloc] init] or [class copy] come with a retention count of 1 • By convention, all other objects are owned by others and likely come autoreleased • They will be automatically destroyed on next wait loop unless otherwise retained • If you create and own an object in a method whose name does not start with new, init or copy, make sure you autorelease it before returning COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 22
- (void) dealloc { } • First (optional step) is to run any logic cleanup code • Second step is to release any retained objects. • [object release], not [object deallocate]. • Do not release the deallocated object itself (no [self release]) • Final step is to call [super dealloc] COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 23
What to release in dealloc • All of your iVars • This includes all IBOutlets • Any other retained objects • Better practice is to release or autorelease non-iVar objects closer to where they are being used after finishing with them. COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 24
Use Your @properties • Release your IBOutlets by setting them to nil. • Do not use myOutlet = nil; as that will likely leak memory. • Use self.myOutlet = nil; • Why not to use [myOutlet release]; • Still leaves myOutlet pointing to a likely destroyed object. • Releasing a destroyed object raises an error • Releasing a nil object is fine COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 25
Application Life Cycle Events • You will soon learn about • viewDidLoad • viewDidUnload • Application:didFinishLaunchingWithOptions: • awakeFromNib • etc • You will want to release IBOutlets in viewDidUnload and dealloc. • Put set of “self.myOutlet=nil” statements in private method • Call method from viewDidUnload and dealloc. • Remember no problem sending messages to nil • Cannot release a destroyed object COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 26
Common Problems • NSMutableArray *mutableArray; • [mutableArray addObject:anObject]; COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 27
Common Problems • Foo.h Foo.m • @interface { • double waitingOperand; • NSString *waitingOperation; • NSArray *expression; • } • -(void) dealloc { • [waitingOperand dealloc]; • [NSArray dealloc]; • [super dealloc]; • } COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11
Common Problems • Foo.h Foo.m • @interface { • double waitingOperand; • NSString *waitingOperation; • NSArray *expression; • } • -(void) dealloc { • [waitingOperand release]; • [NSArray release]; • [super release]; • } COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11
Common Problems • - (NSArray *) expressionArray:(NSMutableArray *)expression { • NSArray *result = [expression copy]; • return result; • } COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 30
Common Problems • - (viewDidLoad){ • CalculatorBrain *brain=[[CalculatorBrain alloc] init]; • return brain; • } COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 31
Common Problems • - (viewDidUnload){ • display=nil; • } COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 32
NSZombieEnabled • Product Edit Scheme Arguments tab • Under Environment Variables, add • NSZombieEnabled with value YES • You will be notified when you try to use an object which has been deallocated. COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 33
Assignment #2 Common Problems • Not releasing all iVars in dealloc • On extra credit, setting myOutlet=nil rather than self.myOutlet=nil • Using unallocated or initialized variables • NSMutableString *aMutableString; does not allocate it. It may reserve space for the pointer, but nothing has yet been created. • Using nondescriptive iVar names • OK to use aString inside method, but not as iVar • Releasing iVars other than IBOutlets in viewDidUnload COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 34
Assignment #2 Common Problems • Doing nothing with property list functions • Don’t try to game assignment. Some things are there purely for pedagogical value. • Code that crashed (OK if you tell me it is a work in progress) • Not using all possible appropriate properties • Deallocing objects rather than releasing them • Returning a non-autoreleased object from a method that does not start with init, copy or new. • Confusing names. Use aSet rather than set. COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 35
Calculator View Controller COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 36
Calculator Brain COMP 446 / ELEC 446 - Week 4 SEC - 9/14/11 37