iOS Memory management & Navigation

1,386 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,386
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

iOS Memory management & Navigation

  1. 1. WAKE UPSunday, January 15, 12
  2. 2. Sunday, January 15, 12
  3. 3. HIG (HUMAN INTERFACE GUIDELINE) • Прегърни Платформата и Хюман Интерфейс Принципите • Ясни дефиниции: - какво? - на кого?Sunday, January 15, 12
  4. 4. ПРЕЖИВЯВАНЕТО НА ПОТРЕБИТЕЛЯ • поведение • изглед • начало и край • употреба на бутони • употреба на жестовеSunday, January 15, 12
  5. 5. RESOURCES Navigation of ViewControllersSunday, January 15, 12
  6. 6. RESOURCES • images, icons • Has 90° corners • shine or gloss • not use alpha transparency • not include a drop shadowSunday, January 15, 12
  7. 7. Sunday, January 15, 12
  8. 8. CUSTOM ARTWORKSunday, January 15, 12
  9. 9. APPLICATION ICONS iPhone 2G/3G iPhone 4G iPad 57 x 57 114 x 114 72 x 72 App icon Icon.png Icon@2x.png Icon~ipad.png 512 x 512 Awtwork - - iTunesArtwork/.png/ 320 x 480 640 x 960 320 x 480 Loading image Default.png Default@2x.png Default~ipad.png 29 x 29 29 x 29 29 x 29 Settings icon Icon-settings.png / Icon-settings.png / Icon-settings.png / Icon-Small.png Icon-Small.png Icon-Small.png 29 x 29 58 x 58 64 x 64 Documents icon Icon-doc.png Icon-doc@2x.png Icon-doc~ipad.png 50 X 50 Spotlight search - - Icon-spot~ipad.png/Icon- Small-50.pngSunday, January 15, 12
  10. 10. IMPORT IN XCODESunday, January 15, 12
  11. 11. THE MEMORY MANAGEMENTSunday, January 15, 12
  12. 12. АДРЕСИ И СТОЙНОСТИ assign = store addressSunday, January 15, 12
  13. 13. RETAINCOUNTSunday, January 15, 12
  14. 14. Copying Collections COPYING DEEP VS SHALLOW There are two kinds of object copying: shallow copies and deep copies. The normal copy is a shallow copy that produces a new collection that shares ownership of the objects with the original. Deep copies create new objects from the originals and add those to the new collection. This difference is illustrated by Figure 1. objects retain deep copies Figure 1 Shallow copies and objects copy Array 1 Array 2 Array 1 Array 2 0 Object A 0 0 Object A 0 Object A 1 Object B 1 1 Object B 1 Object B 2 Object C 2 2 Object C 2 Object C 3 Object D 3 3 Object D 3 Object D 4 Object E 4 4 Object E 4 Object E Shallow copy Deep copy NSDictionary *shallowCopyDict=[[NSDictionary alloc] NSArray *deepCopyArray=[[NSArray alloc] initWithArray: Shallow Copies initWithDictionary:someDictionary copyItems: NO]; someArray copyItems: YES]; There are a number of ways to make a shallow copy of a collection. When you create a shallow copy, theSunday, January 15, 12 objects in the original collection are sent a retain message and the pointers are copied to the new collection.
  15. 15. RETAIN / RELEASE Application memory management is the process of allocating memory during your program’s runtime, using it, and freeing it when you are done with it. A well-‐written program uses as little memory as possible. In Objective-‐C, it can also be seen as a way of distributing ownership of limited memory resources among many pieces of data and code. When you have finished working through this guide, you will have the knowledge you need to manage your application’s memory by explicitly managing the life cycle of objects and freeing them when they are no longer needed. Although memory management is typically considered at the level of an individual object, your goal is actually A well-written program uses as little memory as possible. to manage object graphs. You want to make sure that you have no more objects in memory than you actually need. Class A Class B Class A Class B alloc/init retain release release Destroyed Retain count = 1 2 2 2 1 0 copy Destroyed 1 0 Class C release Class CSunday, January 15, 12
  16. 16. have cyclical references—that is, they have a strong reference to each other (either directly chain of other objects each with a strong reference to the next leading back to the first). The object relationships shown in Figure 1 (page 17) illustrate a potential retain cycle. The D has a Page object for each page in the document. Each Page object has a property that kee RETAIN CYCLES document it is in. If the Document object has a strong reference to the Page object and the a strong reference to the Document object, neither object can ever be deallocated. The Docu count cannot become zero until the Page object is released, and the Page object won’t be r Document object is deallocated. Figure 1 An illustration of cyclical references Document page don’t Page retain retain parent paragraph Paragraph retain don’t retain text parentSunday, January 15, 12 Use Weak References to Avoid Retain Cycles
  17. 17. THE MEMORY MANAGEMENTSunday, January 15, 12
  18. 18. My W AY THE MEMORY MANAGEMENTSunday, January 15, 12
  19. 19. MEMORY MANAGEMENT objects to be created /UIView/ 0 0 0 View1 View2 View3 subviews nil nil nil /NSArray/ +1 +1 0 +1 View4 retain addSubview alloc /ownership/ nilSunday, January 15, 12
  20. 20. MEMORY MANAGEMENT UIView *....= [[[UIView alloc] init] autorelease]; +1 +1 +1 1 1 1 View1 View2 View3 nil nil nil +1 +1 +1 1 +1 View4 retain addSubview alloc /ownership/ nilSunday, January 15, 12
  21. 21. MEMORY MANAGEMENT Form view hierarchy +1 +1 +1 retain retain 1 3 2 View1 View2 View3 n View2 nil View3 nil nil re tai +1 re ta in +1 +1 2 +1 View4 retain addSubview alloc /ownership/ View2 nilSunday, January 15, 12
  22. 22. MEMORY MANAGEMENT Set view1 as a property /UIVIewController/ Controller @property(nonatomic, retain)UIView *view1; +1 +1 3 3 2 View1 View2 View3 View2 nil View3 nil nil +1 +1 2 +1 View4 retain addSubview alloc /ownership/ View2 nilSunday, January 15, 12
  23. 23. MEMORY MANAGEMENT @property (nonatomic, retain) @property (nonatomic, assign) id delegate; id delegate; assign / weak retain / strong Controller delegate delegate +1 3 4 2 View1 View2 View3 View2 nil View3 nil nil +1 +1 2 +1 View4 retain addSubview alloc /ownership/ View2 nilSunday, January 15, 12
  24. 24. MEMORY MANAGEMENT Controller kills View1 Controller delegate delegate [view2 removeFromSuperview]; retain 3 3 2 View1 View2 View3 -1 nil View3 nil nil re ta in -1 -1 2 -1 View4 dealloc release removeFrom View2 nil SuperviewSunday, January 15, 12
  25. 25. MEMORY MANAGEMENT assign / weak retain / strong Controller delegate delegate -1 -1 retain 1 3 2 View1 View2 View3 - (void)dealloc { [view1 release]; } nil View3 nil nil re ta in -1 -1 2 -1 View4 dealloc release removeFrom View2 nil SuperviewSunday, January 15, 12
  26. 26. MEMORY MANAGEMENT assign / weak retain / strong message to nil delegate delegate is legal retain autorelease nil 3 2 View2 View3 nil View3 nil nil re ta in if ([_delegate respondsToSelector: 2 @selector(requiredMethod)]) { View4 [_delegate requiredMethod]; } View2 nilSunday, January 15, 12
  27. 27. MEMORY MANAGEMENT Kill View4? retain / strong delegate -1 -1 retain 1 2 2 View4 View2 View3 nil View3 nil nil -1 -1 -1 dealloc release removeFrom SuperviewSunday, January 15, 12
  28. 28. RETAIN CYCLE LEAK!?! retain delegate retain 2 2 View2 View3 View3 nil nil Няма останали *указатели към обектитеSunday, January 15, 12
  29. 29. MEMORY MANAGEMENT Kill View4? retain / strong [subviews <#cleanUpDelegates#>] delegate -1 retain 2 3 2 View4 View2 View3 View2 nil View3 nil nil -1 -1 -1 dealloc release removeFrom SuperviewSunday, January 15, 12
  30. 30. MEMORY MANAGEMENT Kill View4? -1 -1 -1 1 1 1 View4 View2 View3 nil nil nil -1 -1 -1 They will be autoreleased or released in dealloc dealloc release removeFrom of the owner SuperviewSunday, January 15, 12
  31. 31. MEMORY MANAGEMENT autorelease 0 0 0 View4 View2 View3 nil nil nilSunday, January 15, 12
  32. 32. ПРИМЕРИ if ([defaults objectForKey:@"actorsPlayed"] != nil) { ! ! [_actorsPlayed release]; ! ! _actorsPlayed = nil; ! ! _actorsPlayed = [[defaults objectForKey:@"actorsPlayed"] retain]; ! } NSDictionary *currRecord = [[NSDictionary alloc] ! ! ! ! ! ! ! ! initWithObjects:[NSArray arrayWithObjects: [NSNumber numberWithInt:highScore], _game.playerName, nil] ! ! ! ! ! ! ! ! forKeys:[NSArray arrayWithObjects:@"score", @"name", Array autorelease nil]]; NSMutableArray *diffHighScore = [[NSMutableArray alloc] initWithArray:[_localHiscores objectForKey:key]];Sunday, January 15, 12
  33. 33. NAVIGATION CONTROLLER ОсновиSunday, January 15, 12
  34. 34. NAVIGATION CONTROLLER CHAPTER 3 Navigation Controllers Figure 3-3 The navigation stack UINavigationController Navigation stack viewControllers (NSArray) View controller topViewController visibleViewController Your main responsibility is to push new view controllers onto the stack in response to user actions. Each v controller you push on the navigation stack is responsible for presenting some portion of your applicatSunday, January 15, 12 data. Typically, when the user selects an item in the currently visible view, you create a new view contro
  35. 35. NAVIGATION CONTROLLER NSArray of ViewControllers Root VC VC2 VC3 Visible Top 0 1 2 3 3 • popToRootViewControllerAnimated:<#(BOOL)#> • popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#> • popViewControllerAnimated:<#(BOOL)#> • pushViewController:<#(UIViewController *)#> animated:<#(BOOL)#>Sunday, January 15, 12
  36. 36. XCODESunday, January 15, 12

×