Why? Scaling images on the fly impact performance.
How? If you have no control over the incoming image size, then scale the image outside of the main thread before displaying.
Tag: ios
Dispatch Queues
What? A queue that runs tasks FIFO.
Types?
- Serial – one at a time
- system-provided: main queue
- you can create your own
- Concurrent – 1+ tasks at a time
- system-provided: 4 queues (high, default, low, and background)
- you can create your own, but 4 system ones should be good enough
Make use of the reuseIdentifier
For:
UITableViewCellsUICollectionViewCellsUITableViewHeaderFooterViews
If not reuse, it will create one each time a cell is displayed which impacts scrolling performance.
Remember to reuse
- headers and footers
- supplementary views
Set views as Opaque, if possible
Opaque means no transparency defined. If not set, it will impact performance, especially the animated UIs.
Why? iOS needs to go deeper into the view hierarchy to figure out what color to render.
How? Use Debug\Color Blended Layers option to locate non-opaque views.
NSError
Notes from NSError
domain: the subsystemcode: error code within that subsystemuserInfo: a dictionary, contains all other details
Also available: localizedDescription
Defer keyword
Run the block of code before leaving the current scope. Similar to “finally” in other language’s try/catch block.
KVO
Notify objects when changes to properties of other objects. Very similar to willSet and didSet, but they can be defined outside of the type defination.
observation = observe(
\.objectToObserve.myDate,
options: [.old, .new]
) { object, change in
print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
}
Never block main thread
Why? UI rendering (UIKit) works in the main thread.
How? All heavy lifting (time-consuming) should be moved out of main thread, including:
- Disk I/O
- Networking, e.g., API calls
- Large computations
- Use GCD, and/or Operation
Responder chain
UIControl actions will send events to a chain of responders, if the 1st one doesn’t implement the action, then it goes deeper to the 2nd one, till it’s handled or no more responders in the chain.
Dynamic dispatch
What? Since override is support by Swift, it has to be determined at runtime what methods/properties to call, so an indirect call cannot be avoid.
When performance is important, to minimize and help compiler optimization, use private, final access levels to declare methods/properties.