From aad3af55ca62aecf3c00443edcf0f43e51bee7ee Mon Sep 17 00:00:00 2001 From: 葛建军 <627299690@qq.com> Date: Fri, 13 Apr 2018 15:08:35 +0800 Subject: [PATCH] 适配9.0系统,数据库coredata增加 message.type字段(用来区分订购,请假等) --- ParentAssistant/ParentAssistant.xcodeproj/project.pbxproj | 4 ++-- ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/jun.xcuserdatad/UserInterfaceState.xcuserstate | Bin 225339 -> 0 bytes ParentAssistant/ParentAssistant/AppDelegate.swift | 47 +++++++++++++++++++++++++++++++++++++++++++++-- ParentAssistant/ParentAssistant/Classes/controllers/main/TabBarController.swift | 8 ++++++-- ParentAssistant/ParentAssistant/Classes/controllers/my/Model/MessageManager.swift | 40 +++++++++++++++++++++++++++++++++++----- 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/ParentAssistant/ParentAssistant.xcodeproj/project.pbxproj b/ParentAssistant/ParentAssistant.xcodeproj/project.pbxproj index 899be39..5ec25c8 100644 --- a/ParentAssistant/ParentAssistant.xcodeproj/project.pbxproj +++ b/ParentAssistant/ParentAssistant.xcodeproj/project.pbxproj @@ -1273,7 +1273,7 @@ "$(PROJECT_DIR)/Pods/NIMSDK_LITE/NIMSDK", ); INFOPLIST_FILE = "$(SRCROOT)/ParentAssistant/Supporting Files/Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = net.shunzi.app.ParentAssistant; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -1303,7 +1303,7 @@ "$(PROJECT_DIR)/Pods/NIMSDK_LITE/NIMSDK", ); INFOPLIST_FILE = "$(SRCROOT)/ParentAssistant/Supporting Files/Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = net.shunzi.app.ParentAssistant; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/jun.xcuserdatad/UserInterfaceState.xcuserstate b/ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/jun.xcuserdatad/UserInterfaceState.xcuserstate index 9b8ac44..bb536d0 100644 Binary files a/ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/jun.xcuserdatad/UserInterfaceState.xcuserstate and b/ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/jun.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ParentAssistant/ParentAssistant/AppDelegate.swift b/ParentAssistant/ParentAssistant/AppDelegate.swift index 0eb2c0d..855a31c 100644 --- a/ParentAssistant/ParentAssistant/AppDelegate.swift +++ b/ParentAssistant/ParentAssistant/AppDelegate.swift @@ -201,11 +201,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. // Saves changes in the application's managed object context before the application terminates. - self.saveContext() + if #available(iOS 10.0, *) { + self.saveContext() + } else { + // Fallback on earlier versions + } } // MARK: - Core Data stack + @available(iOS 10.0, *) lazy var persistentContainer: NSPersistentContainer = { /* The persistent container for the application. This implementation @@ -232,9 +237,47 @@ class AppDelegate: UIResponder, UIApplicationDelegate { }) return container }() - + + lazy var applicationDocumentsDirectory: URL = { + let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) + return urls[urls.count-1] + }() + + lazy var managedObjectModel: NSManagedObjectModel = { + let modelURL = Bundle.main.url(forResource: "ParentAssistant", withExtension: "momd")! + return NSManagedObjectModel(contentsOf: modelURL)! + }() + + lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { + let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) + let url = self.applicationDocumentsDirectory.appendingPathComponent("ParentAssistant.sqlite") + var failureReason = "There was an error creating or loading the application's saved data." + do { + try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil) + } catch { + // Report any error we got. + var dict = [String: AnyObject]() + dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject? + dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject? + + dict[NSUnderlyingErrorKey] = error as NSError + let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) + NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") + abort() + } + + return coordinator + }() + + lazy var managedObjectContext: NSManagedObjectContext = { + let coordinator = self.persistentStoreCoordinator + var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) + managedObjectContext.persistentStoreCoordinator = coordinator + return managedObjectContext + }() // MARK: - Core Data Saving support + @available(iOS 10.0, *) func saveContext () { let context = persistentContainer.viewContext if context.hasChanges { diff --git a/ParentAssistant/ParentAssistant/Classes/controllers/main/TabBarController.swift b/ParentAssistant/ParentAssistant/Classes/controllers/main/TabBarController.swift index 2fdb5c6..2e3acae 100644 --- a/ParentAssistant/ParentAssistant/Classes/controllers/main/TabBarController.swift +++ b/ParentAssistant/ParentAssistant/Classes/controllers/main/TabBarController.swift @@ -46,8 +46,12 @@ class TabBarController: UITabBarController { if UIApplication.appVersion() < version{ let alert = UIAlertController(title: "发现新版本", message: dataInformatica["releaseNotes"]! as? String, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "前往更新", style: .default, handler: { (action) in - // UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id1271291794")!) - UIApplication.shared.open(URL(string: "itms-apps://itunes.apple.com/app/id1357945086")!, options: [:], completionHandler: nil) + if #available(iOS 10.0, *) { + UIApplication.shared.open(URL(string: "itms-apps://itunes.apple.com/app/id1357945086")!, options: [:], completionHandler: nil) + } else { + // Fallback on earlier versions + UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id1271291794")!) + } })) alert.addAction(UIAlertAction(title: "以后再说", style: .cancel, handler: { (action) in diff --git a/ParentAssistant/ParentAssistant/Classes/controllers/my/Model/MessageManager.swift b/ParentAssistant/ParentAssistant/Classes/controllers/my/Model/MessageManager.swift index 4cbe6b2..1e5ee2d 100644 --- a/ParentAssistant/ParentAssistant/Classes/controllers/my/Model/MessageManager.swift +++ b/ParentAssistant/ParentAssistant/Classes/controllers/my/Model/MessageManager.swift @@ -12,7 +12,13 @@ extension NSManagedObject { static func creatWith(identifier:String)->NSManagedObject { //获取管理的数据上下文 对象 - let context = appDelegate.persistentContainer.viewContext + var context:NSManagedObjectContext! + if #available(iOS 10.0, *) { + context = appDelegate.persistentContainer.viewContext + } else { + // Fallback on earlier versions + context = appDelegate.managedObjectContext + } //创建对象 let obj = NSEntityDescription.insertNewObject(forEntityName: identifier,into: context) return obj @@ -20,7 +26,13 @@ extension NSManagedObject { //查询数据操作 static func fetchModel(identifier:String,ownid:String)->[Any]{ //获取管理的数据上下文 对象 - let context = appDelegate.persistentContainer.viewContext + var context:NSManagedObjectContext! + if #available(iOS 10.0, *) { + context = appDelegate.persistentContainer.viewContext + } else { + // Fallback on earlier versions + context = appDelegate.managedObjectContext + } //声明数据的请求 let fetchRequest = NSFetchRequest(entityName:identifier) fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)] @@ -43,7 +55,13 @@ extension NSManagedObject { //查询数据操作 static func fetchAllUnReadModel(identifier:String,ownid:String)->[Any]{ //获取管理的数据上下文 对象 - let context = appDelegate.persistentContainer.viewContext + var context:NSManagedObjectContext! + if #available(iOS 10.0, *) { + context = appDelegate.persistentContainer.viewContext + } else { + // Fallback on earlier versions + context = appDelegate.managedObjectContext + } //声明数据的请求 let fetchRequest = NSFetchRequest(entityName:identifier) fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)] @@ -66,7 +84,13 @@ extension NSManagedObject { func save(){ //获取管理的数据上下文 对象 - let context = appDelegate.persistentContainer.viewContext + var context:NSManagedObjectContext! + if #available(iOS 10.0, *) { + context = appDelegate.persistentContainer.viewContext + } else { + // Fallback on earlier versions + context = appDelegate.managedObjectContext + } //保存 do { try context.save() @@ -77,7 +101,13 @@ extension NSManagedObject { } func delete(){ //获取管理的数据上下文 对象 - let context = appDelegate.persistentContainer.viewContext + var context:NSManagedObjectContext! + if #available(iOS 10.0, *) { + context = appDelegate.persistentContainer.viewContext + } else { + // Fallback on earlier versions + context = appDelegate.managedObjectContext + } context.delete(self) //重新保存-更新到数据库 save() -- libgit2 0.21.0