From bc443aa98d0cd13a83dadbead6c31157c941f5cc Mon Sep 17 00:00:00 2001 From: Chris Li Date: Sun, 18 Sep 2016 15:20:04 -0400 Subject: [PATCH] cancel when force quit --- Kiwix-iOS/AppDelegate.swift | 4 ++-- Kiwix-iOS/Controller/Alerts.swift | 2 +- Kiwix-iOS/Info.plist | 2 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- Kiwix/Network/Network.swift | 18 ++++++++++-------- Kiwix/Operations/ScanLocalBookOperation.swift | 16 ++++++++-------- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Kiwix-iOS/AppDelegate.swift b/Kiwix-iOS/AppDelegate.swift index 62f2d173..26a09ce0 100644 --- a/Kiwix-iOS/AppDelegate.swift +++ b/Kiwix-iOS/AppDelegate.swift @@ -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 diff --git a/Kiwix-iOS/Controller/Alerts.swift b/Kiwix-iOS/Controller/Alerts.swift index f00d3cee..4aca557a 100644 --- a/Kiwix-iOS/Controller/Alerts.swift +++ b/Kiwix-iOS/Controller/Alerts.swift @@ -43,7 +43,7 @@ class RemoveBookConfirmationAlert: AlertOperation { let operation = RemoveBookOperation(bookID: bookID) GlobalQueue.shared.addOperation(operation) } - addActionWithTitle(LocalizedStrings.ok) + addActionWithTitle(LocalizedStrings.cancel) preferredAction = actions[0] } } diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 29e4eb6e..1711cb97 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.725 + 1.8.758 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index a31e582b..768b148b 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.8.728 + 1.8.761 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix/Network/Network.swift b/Kiwix/Network/Network.swift index 0c1e0386..5d38d47b 100644 --- a/Kiwix/Network/Network.swift +++ b/Kiwix/Network/Network.swift @@ -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} diff --git a/Kiwix/Operations/ScanLocalBookOperation.swift b/Kiwix/Operations/ScanLocalBookOperation.swift index 82fb2ddf..0caacb29 100644 --- a/Kiwix/Operations/ScanLocalBookOperation.swift +++ b/Kiwix/Operations/ScanLocalBookOperation.swift @@ -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 {