From 74cabd5d4775a33325eab9a490055ca9ab8deb47 Mon Sep 17 00:00:00 2001 From: Yannik Date: Fri, 14 Jun 2019 18:55:23 +0200 Subject: [PATCH] Add message caching for outgoing messages --- MessageEntitie+CoreDataClass.swift | 15 ++ MessageEntitie+CoreDataProperties.swift | 26 +++ .../contents.xcworkspacedata | 6 + PulsePartner/AppDelegate.swift | 43 +++++ PulsePartner/Base.lproj/Main.storyboard | 8 - .../Controller/ChatViewController.swift | 2 +- .../Controller/MainViewController.swift | 17 +- PulsePartner/Model/Chat/Chat.swift | 20 ++- .../Chat.xcdatamodel/contents | 6 +- PulsePartner/Model/Chat/ChatManager.swift | 160 ++++++++++++++---- .../ChatViewControllerExtension.swift | 1 + PulsePartner/View/MatchCell.swift | 32 ++-- 12 files changed, 251 insertions(+), 85 deletions(-) create mode 100644 MessageEntitie+CoreDataClass.swift create mode 100644 MessageEntitie+CoreDataProperties.swift diff --git a/MessageEntitie+CoreDataClass.swift b/MessageEntitie+CoreDataClass.swift new file mode 100644 index 0000000..21140e6 --- /dev/null +++ b/MessageEntitie+CoreDataClass.swift @@ -0,0 +1,15 @@ +// +// MessageEntitie+CoreDataClass.swift +// +// +// Created by yannik grotkop on 13.06.19. +// +// + +import Foundation +import CoreData + +@objc(MessageEntitie) +public class MessageEntitie: NSManagedObject { + +} diff --git a/MessageEntitie+CoreDataProperties.swift b/MessageEntitie+CoreDataProperties.swift new file mode 100644 index 0000000..5968403 --- /dev/null +++ b/MessageEntitie+CoreDataProperties.swift @@ -0,0 +1,26 @@ +// +// MessageEntitie+CoreDataProperties.swift +// +// +// Created by yannik grotkop on 13.06.19. +// +// + +import Foundation +import CoreData + + +extension MessageEntitie { + + @nonobjc public class func fetchRequest() -> NSFetchRequest { + return NSFetchRequest(entityName: "MessageEntitie") + } + + @NSManaged public var chatID: String? + @NSManaged public var date: NSDate? + @NSManaged public var matchID: String? + @NSManaged public var message: String? + @NSManaged public var ownerID: String? + @NSManaged public var read: Bool + +} diff --git a/PulsePartner.xcworkspace/contents.xcworkspacedata b/PulsePartner.xcworkspace/contents.xcworkspacedata index 5821056..c3b3744 100644 --- a/PulsePartner.xcworkspace/contents.xcworkspacedata +++ b/PulsePartner.xcworkspace/contents.xcworkspacedata @@ -1,6 +1,12 @@ + + + + diff --git a/PulsePartner/AppDelegate.swift b/PulsePartner/AppDelegate.swift index 451946c..59b12ba 100644 --- a/PulsePartner/AppDelegate.swift +++ b/PulsePartner/AppDelegate.swift @@ -9,6 +9,7 @@ import UIKit import Firebase import Kingfisher +import CoreData @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { @@ -55,4 +56,46 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // See also applicationDidEnterBackground:. } + lazy var persistentContainer: NSPersistentContainer = { + /* + The persistent container for the application. This implementation + creates and returns a container, having loaded the store for the + application to it. This property is optional since there are legitimate + error conditions that could cause the creation of the store to fail. + */ + let container = NSPersistentContainer(name: "Chat") + container.loadPersistentStores(completionHandler: { (storeDescription, error) in + if let error = error as NSError? { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + + /* + Typical reasons for an error here include: + * The parent directory does not exist, cannot be created, or disallows writing. + * The persistent store is not accessible, due to permissions or data protection when the device is locked. + * The device is out of space. + * The store could not be migrated to the current model version. + Check the error message to determine what the actual problem was. + */ + fatalError("Unresolved error \(error), \(error.userInfo)") + } + }) + return container + }() + + // MARK: - Core Data Saving support + func saveContext () { + let context = persistentContainer.viewContext + if context.hasChanges { + do { + try context.save() + } catch { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + let nserror = error as NSError + fatalError("Unresolved error \(nserror), \(nserror.userInfo)") + } + } + } + } diff --git a/PulsePartner/Base.lproj/Main.storyboard b/PulsePartner/Base.lproj/Main.storyboard index 9c1ae36..c31dd79 100644 --- a/PulsePartner/Base.lproj/Main.storyboard +++ b/PulsePartner/Base.lproj/Main.storyboard @@ -130,13 +130,6 @@ -