High quality iOS development

Post on 15-May-2015

2.278 views 2 download

Tags:

description

For QCon 2011 Beijing

Transcript of High quality iOS development

High Quality iOS Development

@robinluIN-SRC Studio

Saturday, April 9, 2011

Quality

Saturday, April 9, 2011

Topics

• Memory management

• Tools

• Crash Reports

• Test

Saturday, April 9, 2011

Memory Management

Saturday, April 9, 2011

Memory Management

alloccopyretain

objectreference

countrelease

+1 -1

0

dealloc

Saturday, April 9, 2011

OwnershipDeclare ownership

• create / copy

• instance variable

• container

• multi-thread

• ...

Release ownership

• don’t need anymore

• dealloc of owner

Saturday, April 9, 2011

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

Problems

• Memory Leak

• Over-release

Saturday, April 9, 2011

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

Pitfalls

• Implicit Onwership

• Circular Reference

Saturday, April 9, 2011

Implicit Ownership

• NSTimer

• Delayed performance of selector

• Thread

• IBOutlet

Saturday, April 9, 2011

Circular Reference

A retains B

B retains C

C retains A

A

C B

Break it with weak references

Saturday, April 9, 2011

General Practices

• Instance Variables

• Local Variables

Saturday, April 9, 2011

Instance Variable

get ownership in release ownership in

init dealloc

viewDidLoadviewDidUnload

dealloc

settersetterdealloc

Saturday, April 9, 2011

Local Variable

Always release or autorelease in the same scope

Saturday, April 9, 2011

Summary

• Ownership

• Rules

• Pitfalls

• Practices

Saturday, April 9, 2011

Analyze

Saturday, April 9, 2011

Analyze

• Clang Static Analyzer

• More powerful than compiler warnings

• Bug finder

• memory leaks

• dead store

• logic error

• ...

Saturday, April 9, 2011

Memory Leaks

Saturday, April 9, 2011

Potential Memory Leak

Saturday, April 9, 2011

Dead Store

Value stored to ‘matchedRange’ during initialization is never read

Saturday, April 9, 2011

Logic error

Saturday, April 9, 2011

Run Analyze

Saturday, April 9, 2011

Instruments

Saturday, April 9, 2011

Instruments

• Memory

• Performance

• ...

Saturday, April 9, 2011

Memory

• Memory Leaks

• Abnormal Allocation

• Heapshots

• Over Release

• Zombies

• Memory Clean up

• VM Tracker

Saturday, April 9, 2011

Time Profiling

• Sampling

• Record Mode

• Immediate mode

• Deferred mode

Saturday, April 9, 2011

Crash Reports

Saturday, April 9, 2011

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

Saturday, April 9, 2011

Xcode Organizer

Get crash reports from devices

Saturday, April 9, 2011

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

iTunes Connect

Get users’ crash reports after release

Saturday, April 9, 2011

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

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

Test

Saturday, April 9, 2011

Test

• Unit Test

• Beta Test

• Automating UI Test

Saturday, April 9, 2011

Unit Test

• Unit Test Target

• How easy to test: M > C > V

Saturday, April 9, 2011

Automating UI Test

• Instruments

• Accessibility

• Javascript

Saturday, April 9, 2011

Beta Test

• Ad Hoc

• Build suggestion:

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

Saturday, April 9, 2011

Ad Hoc Deploy

• Deploy via internet

• Hockey

• TestFlightApp.com

• dropbox

Saturday, April 9, 2011

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

Thanks!

Saturday, April 9, 2011