main
Laurent 3 years ago
parent 7690ea7d8c
commit 514794f91d
  1. 17
      Notes/AppDelegate.swift
  2. 17
      Notes/NoteViewController.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
}()

@ -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,8 +80,10 @@ class NoteViewController : UIViewController, UITextViewDelegate {
@objc fileprivate func _storeRemoteChange(notification: Notification) {
print("_storeRemoteChange...")
DispatchQueue.main.async {
self._loadLastNote()
}
}
func textViewDidChange(_ textView: UITextView) {
PreferencesStorage.main.requestStorage(filename: "main.txt", content: textView.text)
@ -155,4 +164,8 @@ class NoteViewController : UIViewController, UITextViewDelegate {
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}

Loading…
Cancel
Save