Friday, May 31, 2019

@autoreleasepool Uses in 2019 Swift

Bruno Rocha:

To put it short, autoreleasepool is still useful in iOS/Swift development as there are still legacy Obj-C classes in UIKit and Foundation that call autorelease, but you likely don’t need to worry about it when dealing with Swift classes due to ARC’s optimizations.

Presumably, pure Swift objects can also be autoreleased when manipulated by Objective-C code. I still find autoreleasepool to be useful, both to reduce peak memory use and to ensure that resources associated with objects will be cleaned up at a particular time.

One pain point has been that XCTestCase doesn’t seem to use pools around its setUp() and tearDown() methods. This led to log messages from Core Data complaining that the SQLite database had been deleted out from under it when the test was over. An autoreleased controller had been retaining the persistent store coordinator.

I ended up making a subclass of XCTestCase with its own lifecycle methods that run inside autorelease pools and are allowed to throw errors.

Comments RSS · Twitter

Leave a Comment