Difference between revisions of "Swift Tips and Best Practice"
From Hawk Wiki
(Created page with "===NSData(contentWithUrl: url)=== Do not use this for network request. Because it is synchronized and it may block your UI a tens of seconds. ===Run Loop=== It's the main thread ...") |
m (→Do not pass objects between threads) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | ==Multi threading pro tips== | ||
===NSData(contentWithUrl: url)=== | ===NSData(contentWithUrl: url)=== | ||
Do not use this for network request. Because it is synchronized and it may block your UI a tens of seconds. | Do not use this for network request. Because it is synchronized and it may block your UI a tens of seconds. | ||
Line 13: | Line 14: | ||
old method | old method | ||
Clumsy and poor. Do not use. | Clumsy and poor. Do not use. | ||
− | ==== | + | ====Grand Central Dispatch==== |
* Abstract thread pool pattern | * Abstract thread pool pattern | ||
* Better performance | * Better performance | ||
+ | ====Do not pass objects between threads==== | ||
+ | Instead, use an observer. Send notification when the object is updated in the sub-thread, then in the main thread, subscribe to the notification, fetch data when got notification. |
Latest revision as of 03:45, 17 March 2015
Contents
Multi threading pro tips
NSData(contentWithUrl: url)
Do not use this for network request. Because it is synchronized and it may block your UI a tens of seconds.
Run Loop
It's the main thread run()
- Handle UI Input
- View Drawing
- Animation
- More
You have 17ms to do stuff in the main thread.
Threading
If you want to some intensive computation. Use another thread.
NSThread
old method Clumsy and poor. Do not use.
Grand Central Dispatch
- Abstract thread pool pattern
- Better performance
Do not pass objects between threads
Instead, use an observer. Send notification when the object is updated in the sub-thread, then in the main thread, subscribe to the notification, fetch data when got notification.