High quality iOS development

43
High Quality iOS Development @robinlu IN-SRC Studio Saturday, April 9, 2011

description

For QCon 2011 Beijing

Transcript of High quality iOS development

Page 1: High quality iOS development

High Quality iOS Development

@robinluIN-SRC Studio

Saturday, April 9, 2011

Page 2: High quality iOS development

Quality

Saturday, April 9, 2011

Page 3: High quality iOS development

Topics

• Memory management

• Tools

• Crash Reports

• Test

Saturday, April 9, 2011

Page 4: High quality iOS development

Memory Management

Saturday, April 9, 2011

Page 5: High quality iOS development

Memory Management

alloccopyretain

objectreference

countrelease

+1 -1

0

dealloc

Saturday, April 9, 2011

Page 6: High quality iOS development

OwnershipDeclare ownership

• create / copy

• instance variable

• container

• multi-thread

• ...

Release ownership

• don’t need anymore

• dealloc of owner

Saturday, April 9, 2011

Page 7: High quality iOS development

Autorelease

• give the ownership to autorelease pool

• default autorelease pool created by AppKit

• life cycle = event cycle

• You can create your own autorelease pool

• Don’t over use autorelease

Saturday, April 9, 2011

Page 8: High quality iOS development

Problems

• Memory Leak

• Over-release

Saturday, April 9, 2011

Page 9: High quality iOS development

RulesYou only release or autorelease

objects you own

• If you own the object by alloc, copy or retain, you have to release or autorelease

• If the object is not owned by you, don’t call release or autorelease

Saturday, April 9, 2011

Page 10: High quality iOS development

Pitfalls

• Implicit Onwership

• Circular Reference

Saturday, April 9, 2011

Page 11: High quality iOS development

Implicit Ownership

• NSTimer

• Delayed performance of selector

• Thread

• IBOutlet

Saturday, April 9, 2011

Page 12: High quality iOS development

Circular Reference

A retains B

B retains C

C retains A

A

C B

Break it with weak references

Saturday, April 9, 2011

Page 13: High quality iOS development

General Practices

• Instance Variables

• Local Variables

Saturday, April 9, 2011

Page 14: High quality iOS development

Instance Variable

get ownership in release ownership in

init dealloc

viewDidLoadviewDidUnload

dealloc

settersetterdealloc

Saturday, April 9, 2011

Page 15: High quality iOS development

Local Variable

Always release or autorelease in the same scope

Saturday, April 9, 2011

Page 16: High quality iOS development

Summary

• Ownership

• Rules

• Pitfalls

• Practices

Saturday, April 9, 2011

Page 17: High quality iOS development

Analyze

Saturday, April 9, 2011

Page 18: High quality iOS development

Analyze

• Clang Static Analyzer

• More powerful than compiler warnings

• Bug finder

• memory leaks

• dead store

• logic error

• ...

Saturday, April 9, 2011

Page 19: High quality iOS development

Memory Leaks

Saturday, April 9, 2011

Page 20: High quality iOS development

Potential Memory Leak

Saturday, April 9, 2011

Page 21: High quality iOS development

Dead Store

Value stored to ‘matchedRange’ during initialization is never read

Saturday, April 9, 2011

Page 22: High quality iOS development

Logic error

Saturday, April 9, 2011

Page 23: High quality iOS development

Run Analyze

Saturday, April 9, 2011

Page 24: High quality iOS development

Instruments

Saturday, April 9, 2011

Page 25: High quality iOS development

Instruments

• Memory

• Performance

• ...

Saturday, April 9, 2011

Page 26: High quality iOS development

Memory

• Memory Leaks

• Abnormal Allocation

• Heapshots

• Over Release

• Zombies

• Memory Clean up

• VM Tracker

Saturday, April 9, 2011

Page 27: High quality iOS development

Time Profiling

• Sampling

• Record Mode

• Immediate mode

• Deferred mode

Saturday, April 9, 2011

Page 28: High quality iOS development

Crash Reports

Saturday, April 9, 2011

Page 29: High quality iOS development

Crash Reports

• What’s crash reports?

• Where to get crash reports?

• How to read crash reports?

• Generate crash reports by 3rd party lib

Saturday, April 9, 2011

Page 30: High quality iOS development

Saturday, April 9, 2011

Page 31: High quality iOS development

Xcode Organizer

Get crash reports from devices

Saturday, April 9, 2011

Page 32: High quality iOS development

Local Directories• Mac OS X: ~/Library/Logs/CrashReporter/

MobileDevice/ <DEVICE_NAME>

• Windows XP: C:\Documents and Settings\<USERNAME>\Application Data\Apple Computer\Logs\CrashReporter\MobileDevice \<DEVICE_NAME>

• Windows Vista + 7: C:\Users\<USERNAME>\AppData\Roaming\Apple Computer\Logs\CrashReporter\MobileDevice\<DEVICE_NAME>

Saturday, April 9, 2011

Page 33: High quality iOS development

iTunes Connect

Get users’ crash reports after release

Saturday, April 9, 2011

Page 34: High quality iOS development

Crash Types

• EXC_BAD_ACCESS (SIGBUS or SIGSEGV)

• EXC_CRASH (SIGABRT)

• Low Memory

• 00000020

Exception Codes Type

0x8badf00d Timeout

0xdeadfa11 User Force-Quit

Saturday, April 9, 2011

Page 35: High quality iOS development

Symbols

• Keep symbol before each release

• Xcode Archive

• dSYM file

• Read the report

• Xcode organizer

• atos, symbolicatecrash, symbolizecrashlog

• 3rd party tools - Symbolicator

Saturday, April 9, 2011

Page 36: High quality iOS development

Test

Saturday, April 9, 2011

Page 37: High quality iOS development

Test

• Unit Test

• Beta Test

• Automating UI Test

Saturday, April 9, 2011

Page 38: High quality iOS development

Unit Test

• Unit Test Target

• How easy to test: M > C > V

Saturday, April 9, 2011

Page 39: High quality iOS development

Automating UI Test

• Instruments

• Accessibility

• Javascript

Saturday, April 9, 2011

Page 40: High quality iOS development

Beta Test

• Ad Hoc

• Build suggestion:

Ad Hoc and Distribution share the same bundle id but different from Debug build

Saturday, April 9, 2011

Page 41: High quality iOS development

Ad Hoc Deploy

• Deploy via internet

• Hockey

• TestFlightApp.com

• dropbox

Saturday, April 9, 2011

Page 42: High quality iOS development

Crash Report Lib

• plcrashreporter

• http://code.google.com/p/plcrashreporter/

• can be used in both ad hoc and distribution builds

• output as protobuf-encoded messages

Saturday, April 9, 2011

Page 43: High quality iOS development

Thanks!

Saturday, April 9, 2011