Thursday, June 30, 2011

WWDC2011 Session 323 Introducing Automatic Reference Counting

Summary: Automatic Reference Counting (ARC) dramatically simplifies memory management in Objective-C. This session introduces what ARC is, how it is implemented and how to migrate to ARC. Apple asks every developer to move Objective-C code to ARC.

Problems of memory management:

Many (new) developers do not understand reference counting. They don’t know when to retain/release/autorelease. Memory leaks and the app crashes.

What is ARC?

ARC is automatic object memory management. The compiler adds retain/release calls. It still uses reference counting. ARC is not Garbage collection. ARC is compile-time memory management, not run-time memory management.

If we write a stack, our code without ARC will be:

@implementation Stack { NSMutableArray *_array; }

- (id) init {

if (self = [super init])

_array = [[NSMutableArray array] retain];

return self;

}

- (void) push: (id) x {

[_array addObject: x];

}

- (id) pop {

id x = [[_array lastObject] retain];

[_array removeLastObject];

return [x autorelease];

}

- (void) dealloc { [_array release]; [super dealloc]; }

@end

With ARC:

@implementation Stack { NSMutableArray *_array; }

- (id) init {

if (self = [super init])

_array = [NSMutableArray array];

return self;

}

- (void) push: (id) x {

[_array addObject: x];

}

- (id) pop {

id x = [_array lastObject];

[_array removeLastObject];

return x;

}

@end

How do you switch?

WWDC2011 Learning Notes and Index

WWDC2011videos come out before I finish WWDC2010 videos.

It's time to learn WWDC2011 videos. :-)

Index

WWDC2011 Session 323 Introducing Automatic Reference Counting

Thursday, April 21, 2011

Programming with Core Animation on Mac

Learning notes of Core Animation for Mac OS X and the iPhone

1. The Simplest animation (CABasicAnimation)

Without animation: [theView setFrame:newFrame];

With animation: [[theView animator] setFrame:newFrame];

[theView animator] is the Animator Proxy which is simply finding an animation and then invoking it. The default animation is CABasicAnimation.

2. CAKeyframeAnimation

With CAfeyFrameAnimation, you can define a series of key frames. CoreAnimation system will create animation based on these key frames. For example, we want to create an animation of moving an image to point A, and then B. We only need to create the key frames of A and B. CAKeyframeAnimation could be understood as a series of CABasicAnimation.

// Create the path

NSRect frame = [theView frame];

CGMutablePathRef thePath = CGPathCreateMutable();

CGPathMoveToPoint(thePath, NULL, NSMinX(frame), NSMinY(frame)); //the origin

CGPathAddLineToPoint(thePath, NULL, pointA.x, pointA.y); //Point A

CGPathAddLineToPoint(thePath, NULL, pointB.x, pointB.y); //Point B

// Create an animation

CAKeyframeAnimation *originAnimation = [CAKeyframeAnimation animation];

originAnimation.path = thePath;

originAnimation.duration = 2.0f;

originAnimation.calculationMode = kCAAnimationPaced;

// Add this animation

[theView setAnimations:[NSDictionary dictionaryWithObjectsAndKeys: originAnimation, @”frameOrigin”, nil]];

// Activate this animation

[[theView animator] setFrameOrigin:pointB]; //set the point of the last frame

Monday, April 11, 2011

Add an existing framework in Xcode 4

Steps to add an existing framework in Xcode 4:

1. Click the project in the project navigator.

2. In the project editor, click a target.

3. Select 'Build Phases' tab

4. Expand 'Link Binary With Libraries'

5. Click '+' button to add a framework.

6. Go back to the project navigator, you will see the new framework. You can drag it to 'Frameworks' group.

Friday, April 08, 2011

WWDC2010 Session211 Simplifying iPhone App Development with GCD

GCD Overview

1. GCD is part of libSystem.dylib

2. Available to all Apps.

- #include <dispatch/dispatch.h>

3. GCD API has block-based and function-based variants

- Focus today on block-based API

Introduction to GCD recap

1. Blocks

- dispatch_async()

2. Queues

- Lightweight list of blocks

- Enqueue/dequeue is FIFO

3. dispatch_get_main_queue()

- Main thread/main runloop

4. dispatch_queue_create()

- Automatic helper thread

GCD Advantages

1. Efficiency - More CPU cycles available for your code

2. Better metaphors

- Blocks are easy to use

- Queues are inherently producer/consumer

3. Systemwide perspective

- Only the OS can balance unrelated subsystems

Compatibility

1. Existing threading and synchronization primitives are 100% compatible

2. GCD threads are wrapped POSIX threads

- Do not cancel, exit, kill, join, or detach GCD threads

3. GCD reuses threads

Thursday, April 07, 2011

WWDC2010 Session206 Introducing Blocks and Grand Central Dispatch (2)

Grand Central Dispatch

With GCD, you can make your app responsive. Threading is hard. Using GCD makes it simple and fun. You need not do explicit thread management. Cool!

Keeping your app responsive:

1. Do not block the main thread

2. Move work to another thread

3. Update UI back on main threaad

Code without GCD:

- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {

  // Controller UI callback on main thread

  DTweet *tw = [[DTweet alloc] initWithMsg:msg];

  [tweets addTweet:tw display:YES];

  tw.img = [imageCache getImgFromURL:url];//bottle neck

  [tweets updateTweet:tw display:YES];

  [tw release];

}

Code with GCD:

- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {

  // Controller UI callback on main thread

  DTweet *tw = [[DTweet alloc] initWithMsg:msg];

  [tweets addTweet:tw display:YES];

  dispatch_async(image_queue, ^{

    tw.img = [imageCache getImgFromURL:url];

    dispatch_async(main_queue, ^{

      [tweets updateTweet:tw display:YES];

    });

  });

  [tw release];

}

GCD Queues

1. Lightweight list of blocks

2. Enqueue/dequeue is FIFO

3. Enqueue with dispatch_async()

4. Dequeue by automatic thread or main thread

Main Queue

1. Executes blocks one at a time on main thread

2. Cooperates with the UIKit main run loop

3. dispatch_get_main_queue()