cancel when force quit

This commit is contained in:
Chris Li 2016-09-18 15:20:04 -04:00
parent c8dd4ca05f
commit bc443aa98d
6 changed files with 23 additions and 21 deletions

View File

@ -27,10 +27,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSURLProtocol.registerClass(KiwixURLProtocol)
//Network.shared.restoreProgresses()
Network.shared
// Register notification
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) // Here are the notification permission the app wants
// let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) // Here are the notification permission the app wants
// application.registerUserNotificationSettings(settings)
// Set background refresh interval

View File

@ -43,7 +43,7 @@ class RemoveBookConfirmationAlert: AlertOperation<UIViewController> {
let operation = RemoveBookOperation(bookID: bookID)
GlobalQueue.shared.addOperation(operation)
}
addActionWithTitle(LocalizedStrings.ok)
addActionWithTitle(LocalizedStrings.cancel)
preferredAction = actions[0]
}
}

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.725</string>
<string>1.8.758</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.8.728</string>
<string>1.8.761</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -19,6 +19,7 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
private override init() {
super.init()
queue.delegate = self
session.getAllTasksWithCompletionHandler { _ in }
}
lazy var session: NSURLSession = {
@ -31,25 +32,24 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
// MARK: - OperationQueueDelegate
func operationQueue(queue: OperationQueue, willAddOperation operation: NSOperation) {
print("DEBUG: Network Queue will add " + (operation.name ?? "Unknown OP"))
guard let bookID = operation.name,
let operation = operation as? DownloadBookOperation else {return}
operations[bookID] = operation
}
func operationQueue(queue: OperationQueue, willFinishOperation operation: NSOperation, withErrors errors: [ErrorType]) {}
func operationQueue(queue: OperationQueue, didFinishOperation operation: NSOperation, withErrors errors: [ErrorType]) {
print("DEBUG: Network Queue did finish " + (operation.name ?? "Unknown OP"))
guard let bookID = operation.name else {return}
operations[bookID] = nil
}
func operationQueue(queue: OperationQueue, willFinishOperation operation: NSOperation, withErrors errors: [ErrorType]) {}
func operationQueue(queue: OperationQueue, willProduceOperation operation: NSOperation) {}
// MARK: - NSURLSessionTaskDelegate
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if let error = error {print(error.localizedDescription)}
guard let error = error, let bookID = task.taskDescription else {return}
self.context.performBlockAndWait {
guard let book = Book.fetch(bookID, context: self.context),
@ -60,7 +60,7 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
downloadTask.totalBytesWritten = task.countOfBytesReceived
downloadTask.state = .Paused
// Save resue data to disk
// Save resume data to disk
guard let resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData] as? NSData else {return}
Preference.resumeData[bookID] = resumeData
} else {
@ -86,14 +86,16 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
guard let bookID = downloadTask.taskDescription else {return}
// Save downloaded zim file
// Book object status will be updated by dir scanner
var fileName: String = downloadTask.response?.suggestedFilename ?? bookID
if !fileName.hasSuffix(".zim") {fileName += ".zim"}
guard let destination = NSFileManager.docDirURL.URLByAppendingPathComponent(fileName) else {return}
_ = try? NSFileManager.defaultManager().moveItemAtURL(location, toURL: destination)
// Perform clean up (remove cache and delete download task)
context.performBlock {
// Scanner Operation will updated Book object status
// - Remove cache, if any
// - Delete Download task Object
context.performBlock {
guard let book = Book.fetch(bookID, context: self.context) else {return}
book.removeResumeData()
guard let downloadTask = book.downloadTask else {return}

View File

@ -81,14 +81,14 @@ class ScanLocalBookOperation: Operation {
}
for id in addedZimFileIDs {
guard let reader = ZimMultiReader.shared.readers[id] else {return}
let book: Book? = {
let book = Book.fetch(id, context: NSManagedObjectContext.mainQueueContext)
return book ?? Book.add(reader.metaData, context: NSManagedObjectContext.mainQueueContext)
}()
book?.isLocal = true
book?.hasIndex = reader.hasIndex()
book?.hasPic = !reader.fileURL.absoluteString!.containsString("nopic")
guard let reader = ZimMultiReader.shared.readers[id],
let book: Book = {
let book = Book.fetch(id, context: NSManagedObjectContext.mainQueueContext)
return book ?? Book.add(reader.metaData, context: NSManagedObjectContext.mainQueueContext)
}() else {return}
book.isLocal = true
book.hasIndex = reader.hasIndex()
book.hasPic = !reader.fileURL.absoluteString!.containsString("nopic")
}
for (id, book) in localBooks {