-
-
Save hamzamu/d8306aa1148cf9b844217a3eb5ded52a to your computer and use it in GitHub Desktop.
SwiftUI 2020 + CloudKit + Core Data ZenJournal prototype: https://thezenjournal.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import CoreData | |
let appTransactionAuthorName = "app" | |
@main | |
struct ZenJournalApp: App { | |
var body: some Scene { | |
WindowGroup { | |
NavigationWrapper() | |
.environment(\.managedObjectContext, persistentContainer.viewContext) | |
} | |
} | |
var persistentContainer: NSPersistentCloudKitContainer = { | |
let container = NSPersistentCloudKitContainer(name: "ZenModel") | |
// Enable history tracking and remote notifications | |
guard let description = container.persistentStoreDescriptions.first else { | |
fatalError("###\(#function): Failed to retrieve a persistent store description.") | |
} | |
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) | |
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) | |
let id = "iCloud.com.idealistspace.zenjournal" | |
let options = NSPersistentCloudKitContainerOptions(containerIdentifier: id) | |
description.cloudKitContainerOptions = options | |
container.loadPersistentStores(completionHandler: { (storeDescription, error) in | |
if let error = error as NSError? { | |
fatalError("Unresolved error \(error), \(error.userInfo)") | |
} | |
}) | |
// do { | |
// // Uncomment to do a dry run and print the CK records it'll make | |
//// try container.initializeCloudKitSchema(options: [.dryRun, .printSchema]) | |
// // Uncomment to initialize your schema | |
// try container.initializeCloudKitSchema() | |
// } catch { | |
// print("Unable to initialize CloudKit schema: \(error.localizedDescription)") | |
// } | |
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy | |
container.viewContext.transactionAuthor = appTransactionAuthorName | |
// Pin the viewContext to the current generation token and set it to keep itself up to date with local changes. | |
container.viewContext.automaticallyMergesChangesFromParent = true | |
do { | |
try container.viewContext.setQueryGenerationFrom(.current) | |
} catch { | |
fatalError("###\(#function): Failed to pin viewContext to the current generation:\(error)") | |
} | |
return container | |
}() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CoreData | |
class ZenLog: NSManagedObject, Identifiable { | |
@NSManaged var text: String? | |
@NSManaged var createdAt: Date? | |
} | |
extension ZenLog { | |
// ❇️ The @FetchRequest property wrapper in the ContentView will call this function | |
static func allLogsFetchRequest() -> NSFetchRequest<ZenLog> { | |
let request: NSFetchRequest<ZenLog> = ZenLog.fetchRequest() as! NSFetchRequest<ZenLog> | |
// ❇️ The @FetchRequest property wrapper in the ContentView requires a sort descriptor | |
request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: true)] | |
return request | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment