Learn Courses My Dashboard

Core Data - single Value in Attribute


I am not sure if core data is the right spot for me. I am looking for saving offline values but not in an array. Just single values in attributes. But Core Data likes arrays more I guess.
Is there a workaround just to save one single value in the attribute ?
Firebase lets me store single values OR arrays. In CoreData I can only store arrays in the Entity.
I always have to use a “LIST View” to present the data. I cannot take a Text(var) to show the specific data.
Is CoreData only for arrays ?

Thanks for the explanation.

If you only want to save a single value like that it sound like you should use UserDefaults,

But overall no Core Data isn’t only for arrays / lists of days. You could accomplish it with a single piece of data, but typically if it’s that small just using UserDefaults works

1 Like

I tried to go with "first delete all objects then insert a new one so there will always be just one object in the array. But somehow it does not work. It adds and saves new data, but it doesn’t delete data.

struct ContentView: View {

@Environment(\.managedObjectContext) var moc
@FetchRequest(sortDescriptors: []) var storedata: FetchedResults<Fuel>

@State private var input: String = ""

var body: some View {
    VStack {
       List(storedata, id: \.self) {i in
            HStack {
                Text(i.name ?? "Unknown")
        .frame(height: 200)
            TextField("Insert Name", text: $input)

            Button("Save") {

                let newname  = Fuel(context: moc)
                newname.name = input
                try? moc.save()
        Button("Delete") {

            let newname  = Fuel(context: moc)
                  try moc.save()



struct ContentView_Previews: PreviewProvider {
static var previews: some View {