     var cacheURL: NSURL!
 }
 
+#if os(ios) || os(watchos)
 private struct DiskBackgroundTask {
-    #if os(ios) || os(watchos)
-    var taskIdentifier: UIBackgroundTaskIdentifier
-    #endif
-}
+    var taskIdentifier: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
+    
+    static func start() -> DiskBackgroundTask {
+        var task = DiskBackgroundTask()
+        task.taskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
+            let taskIdentifier = task.taskIdentifier
+            task.taskIdentifier = nil
+            UIApplication.sharedApplication().endBackgroundTask(taskIdentifier)
+        }
+    
+        return task
+    }
+    
+    func end() {
+        let taskIdentifier = self.taskIdentifier
+        self.taskIdentifier = nil
+        UIApplication.sharedApplication().endBackgroundTask(taskIdentifier)
+    }
+}
+#else
+private struct DiskBackgroundTask {
+    static func start() -> DiskBackgroundTask { return DiskBackgroundTask() }
+    func end() {}
+}
+#endif

    }
    
    public func objectForKey(key: String) -> NSData? {
        return nil
    }
    
    public func removeObjectForKey(key: String) {
    }
    
    public func trimBeforeDate(date: NSDate) {
    }
    
    public func removeAllObjects() {

    }
    
    public func setObject(object: NSData?, forKey: String) {
        if let data = object {
            let now = NSDate()
            let task = DiskBackgroundTask.start()
            
            lock()
            guard let fileURL = encodedFileURLForKey(forKey) else { return }
            
            if data.writeToURL(fileURL, atomically: true) {
                setFileModificationDate(now, forURL: fileURL)
                
                let values = try? fileURL.resourceValuesForKeys([ NSURLTotalFileAllocatedSizeKey ])
                if let diskFileSize = values?[NSURLTotalFileAllocatedSizeKey] as? Double {
                    state.sizes[forKey] = diskFileSize
                    state.byteCount += diskFileSize
                }
                
                if let limit = state.maximumSizeInBytes where state.byteCount > limit {
                    trimToSizeByDate(limit, completionHandler: nil)
                }
            }
            
            unlock()
            
            task.end()
        }
        else {
            removeObjectForKey(forKey)
