UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I use the SwiftUI TextEditor view with CloudKit and Core Data

Forums > SwiftUI

For my first SwiftUI app I thought I'd keep it simple - a full screen view that allows the user to enter text which is auto saved and sync'd across devices (similar to the Quick Draft app).

I got the text entry view working easily enough using the SwiftUI TextEditor view bound to a string variable e.g.:

var body: some View {
    NavigationView {
        VStack {
            TextEditor(text: $detail)
                .padding()
        }
        .navigationTitle("Note")
    }
}

The TextEditor view provides all the functionality I need. The difficulty I'm having is in saving and syncing the text using Core Data andCloudKit.

I have a data model with just one entity, Note, containing 2 attributes, detail (String) and timestamp (Date). I don't really need the timestamp attribute but it's there just in case. The entity class codegen is set to 'Class Definition'.

Using the following code I can get the Note detail fetched and displayed in a List view:

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext

    @FetchRequest(
        entity: Note.entity(),
        sortDescriptors: [NSSortDescriptor(keyPath: \Note.timestamp, ascending: true)]
    ) var notes: FetchedResults<Note>

    var body: some View {
        NavigationView {
            List {
                ForEach(notes, id: \.self) { note in
                    Text(note.noteDetail)
                }
            }
            .navigationTitle("Notes")
        }
    }
}

The list displays all Notes if there is more than one, but there should only ever be one Note.

What I want is when the app starts it fetches the one and only Note from CloudKit Core Data. If there is no Note in Core Data, an new empty Note is created. This should only ever happen on the very first launch since the Note is never deleted. The TextEditor view in ContentView should bind to the Note's detail attribute to provide automatic saving etc.

The problem I'm having is how to fetch a single Note from Core Data, creating an empty Note if necessary, and passing the Note to ContentView to bind to the TextEditor view. Is this something done in the App struct and passed to ContentView, or should the fetch be done in ContentView? Also, what's te best approach to only fetch one Note from Core Data and create a new empty one if one does not exist?

Thanks.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.