From 514794f91db07c701cb52ca3e3cca8d8e6da4f55 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 23 Sep 2022 11:09:38 +0200 Subject: [PATCH] update --- Notes/AppDelegate.swift | 17 +++++++++++------ Notes/NoteViewController.swift | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Notes/AppDelegate.swift b/Notes/AppDelegate.swift index 68054c9..f8a93e8 100644 --- a/Notes/AppDelegate.swift +++ b/Notes/AppDelegate.swift @@ -17,12 +17,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - if let content = PreferencesStorage.main.getContent(filename: "main.txt") { - let note = Note(context: AppDelegate.viewContext) - note.content = content - AppDelegate.shared.saveContext() - print("default note created") - } +// if let content = PreferencesStorage.main.getContent(filename: "main.txt") { +// let note = Note(context: AppDelegate.viewContext) +// note.content = content +// AppDelegate.shared.saveContext() +// print("default note created") +// } // Override point for customization after application launch. return true @@ -53,6 +53,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { */ let container = NSPersistentCloudKitContainer(name: "Notes") 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. @@ -68,6 +69,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { fatalError("Unresolved error \(error), \(error.userInfo)") } }) + +// container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy +// container.viewContext.automaticallyMergesChangesFromParent = true + return container }() diff --git a/Notes/NoteViewController.swift b/Notes/NoteViewController.swift index fcff952..3449e93 100644 --- a/Notes/NoteViewController.swift +++ b/Notes/NoteViewController.swift @@ -7,6 +7,7 @@ import Foundation import UIKit +import CoreData class NoteViewController : UIViewController, UITextViewDelegate { @@ -37,7 +38,9 @@ class NoteViewController : UIViewController, UITextViewDelegate { /// Store notifications - NotificationCenter.default.addObserver(self, selector: #selector(self._storeRemoteChange(notification:)), name: .NSPersistentStoreCoordinatorStoresDidChange, object: nil) + print("REGISTER") + + NotificationCenter.default.addObserver(self, selector: #selector(self._storeRemoteChange(notification:)), name: NSPersistentCloudKitContainer.eventChangedNotification, object: nil) /// Keyboard notifications @@ -63,7 +66,11 @@ class NoteViewController : UIViewController, UITextViewDelegate { request.sortDescriptors = [NSSortDescriptor(key: "lastEditDate", ascending: false)] do { - self.note = try AppDelegate.viewContext.fetch(request).first + let notes = try AppDelegate.viewContext.fetch(request) + + print("notes in store : \(notes.count)") + + self.note = notes.first self.textView.text = self.note?.content } catch { print("Fetch error = \(error)") @@ -73,7 +80,9 @@ class NoteViewController : UIViewController, UITextViewDelegate { @objc fileprivate func _storeRemoteChange(notification: Notification) { print("_storeRemoteChange...") - self._loadLastNote() + DispatchQueue.main.async { + self._loadLastNote() + } } func textViewDidChange(_ textView: UITextView) { @@ -155,4 +164,8 @@ class NoteViewController : UIViewController, UITextViewDelegate { } + deinit { + NotificationCenter.default.removeObserver(self) + } + }