HeapInspector

Before I left Germany in November 2014 for 5 weeks going to Indonesia and Thailand, I released HeapInspector, which is an iOS debug tool that monitors the memory heap in your app. You can discover memory leaks, no longer needed living objects and more issues directly on your device without ever starting Instruments. Any suggestions or proposals are very welcome. Or just start a Pull Request.

The idea for the HeapInspector was in my mind for a long time. Since ARC is alive we have less crashes or memory pressure. But you can still do a lot of things wrong (retain cycles, static pointer). I’ve seen memory design mistakes that caused crashes and strange behavior in apps. Those errors are mainly caused by a growing codebase and accompanying architecture problems. Identifying those issues  is not easy and time consuming. Apple Instruments can help to analyze the iOS app for memory issues. That is fine if you have enough time and a cable for tethered debugging with your Mac. But for real life debugging (on trains or subways) Instruments wouldn’t be the ideal solution. For some projects we added a label that displayed the current memory usage in Megabytes. This helped us to identify increasing memory and pressures while using the app in real world conditions (bad 3G and so on).
But to find the main reason (i.e. retain cycles) why the memory increased I needed to run Instruments again.
I asked myself: Is there a way to measure and snapshot the memory footprint on the device without Instruments. YES. I need to override ARC and swizzle retain, release, alloc, dealloc,…
I started digging into Clang’s ARC documentation. Also mike ash’s articles inspired me and helped me a lot to understand what goes on behind the scenes. (See links below)

Some Inspirations, Credits & Thanks

UICollectionViewCell Height Bug

UICollectionView is not the newest Apple stuff. But this bug is really annoying. First I expected it’s something wrong with my code. But after isolating the issue, it became clear, that it is a nasty UIKit bug.

demo

This demo project on github demonstrates the UICollectionViewCell’s height bug when the dataSource (controller) returns a zero height for the 1st sections 1st row cell. The bug occurs with the following setup:

  • the UICollectionsView has 2 sections
  • The 1st section has one row
  • This row has a zero height for the cell
  • Result => the following UICollectionView cells (in bounds) are NOT visible, but the collectionView can be scrolled, looks like that the cells are hidden
  • I filed a bug report: rdar://18120029

Yes, I filed a bug report. As often: No response.

Xcode 6 – View Debugging

Xcode 6 has a really cool feature: View debugging – which means that you can visualize your app’s view hierarchy

  1. Print the whole view hierarchy in a list
  2. See the view stack in 3D
  3. Retrieve super detailed information and properties about your view
  4. ….But…you can’t modify the view’s properties like with Reveal. I assume that Apple will add this feature in the Golden Master of Xcode 6

Continue reading

Xcode 6 and Interface Builder – Cool Stuff

Now I see a lot of benefits in working with IB instead of custom UIView classes. (This should be the end of the good old debate IB vs Code.

  • @IBInspectable which gives you access to your custom class properties & @IBDesignable which updates your interface in IB in realtime without compiling the app and launching simulator. There is an tutorial here which goes into more detail. Add the @IBInspectable in front of your properties and change the value via the didSet observer.
    @IBInspectable var borderWidth:CGFloat = 0.0 {
        didSet {
            layer.borderWidth = borderWidth
        }
        } 

    Interface Builder properties
    from now on you can change the custom property in the Interface Builder.

    Add @IBDesignable in front of the class

    @IBDesignable class YourCustomCell : UITableViewCell {
    ..
    

    See your custom UI in real time without running the app. Note: You need to put the class with @IBDesignable in a framework.

  • iOS Frameworks – Now we don’t need to stick with static libs. Using capsulated frameworks including assets is a big win in iOS 8. It looks like that Apple is encouraging the developers to use frameworks as often as possible (also for UIView components)
  • View debugging:  Reveal like feature to display the view hierarchy  – Now in Xcode you can snapshot the current view hierarchy and inspect the stack in 3D like in Reveal. Continue reading

PunchScrollView – the iOS ScrollView Framework which works like UITableView

PunchScrollView hosted on github is a ScrollView framework for iOS (iPhone, iPad and iPod Touch) which works like the Apple UITableView
[Update from WWDC 2012] Apple introduced the UICollectionViewController in iOS 6 which has the same purpose as PunchScrollView. Because it does the same things I will stop developing PunchScrollView in the near future.[/Update]

  • Easy and fast implementation, almost like the UITableView
  • Perfect for photo slideshows or many text pages
  • Use the benefits of the NSIndexPath like you might know it from the UITableView
  • Easy setup in combination with Core Data
  • Helpful methods, i.e. jump or scroll to a desired page
  • Avoid boilerplate code
  • Save lots of memory with the use of dequeuing

Continue reading