Skip to content

2.0.0

Compare
Choose a tag to compare
@irace irace released this 07 May 14:03
· 10 commits to master since this release

2.0.0 removes all references to UIApplication sharedApplication. As of iOS 8, this method is annotated with NS_EXTENSION_UNAVAILABLE_IOS, meaning that it won’t compile as part of an iOS 8 extension. In order to facilitate TMCache usage inside extensions.

TMCache previously used UIApplication for two different functions:

  • Wrapping work in background tasks, to ensure that it completes even if the user backgrounds the application
  • Listening for UIApplicationDidEnterBackgroundNotification and UIApplicationDidReceiveMemoryWarningNotification, in order to perform some cleanup work

If you still want this behavior, it’s now up to you to implement this behavior in your application code. Thankfully, doing so is extremely straightforward:

Background tasks

Create a class that conforms to TMCacheBackgroundTaskManager. Your implementation will likely look very much like this:

Objective-C:

@interface BackgroundTaskManager: NSObject <TMCacheBackgroundTaskManager>

@implementation BackgroundTaskManger

- (UIBackgroundTaskIdentifier)beginBackgroundTask {
    UIBackgroundTaskIdentifier taskID = UIBackgroundTaskInvalid;

    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:taskID];
    }];

    return taskID;
}

- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)identifier {
    [[UIApplication sharedApplication] endBackgroundTask:identifier];
}

@end

Swift:

class BackgroundTaskManager: NSObject, TMCacheBackgroundTaskManager {
    private let application = UIApplication.sharedApplication()

    func beginBackgroundTask() -> UInt {
        let taskID = UIBackgroundTaskInvalid

        application.beginBackgroundTaskWithExpirationHandler {
            self.application.endBackgroundTask(taskID)
        }

        return UInt(taskID)
    }

    func endBackgroundTask(identifier: UInt) {
        application.endBackgroundTask(Int(identifier))
    }
}

Then, pass an instance of your class to the following TMDiskCache class method:

[TMDiskCache setBackgroundTaskManager:[[BackgroundTaskManager alloc] init]];

Clean-up on memory warning/backgrounding notifications

TMMemoryCache has new public methods that can be called in the event of a memory warning or application backgrounding, in order to easily replicate TMCache 1.X.X behavior:

Objective-C:

[[NSNotificationCenter defaultCenter] addObserver:memoryCache
                                         selector:@selector(handleMemoryWarning)        
                                             name:UIApplicationDidReceiveMemoryWarningNotification     
                                           object:[UIApplication sharedApplication]];

[[NSNotificationCenter defaultCenter] addObserver:memoryCache
                                         selector:@selector(handleApplicationBackgrounding)        
                                             name:UIApplicationDidEnterBackgroundNotification     
                                           object:[UIApplication sharedApplication]];

Swift:

NSNotificationCenter.defaultCenter().addObserver(memoryCache, selector: "handleMemoryWarning", name: UIApplicationDidReceiveMemoryWarningNotification, object: UIApplication.sharedApplication())

NSNotificationCenter.defaultCenter().addObserver(memoryCache, selector: "handleApplicationBackgrounding", name: UIApplicationDidEnterBackgroundNotification, object: UIApplication.sharedApplication())