0

I know there are similiar questions but I have spent hours of trying to find relevant result to my situation without success.

I have got one entity: EntityDate.
The entity has got 2 attributes: date and time

I have a variable "date" that I save as a String to core data like this:



addToCoreData(name: getDateFormatted(), entityName: "EntityDate", key: "date")


func addToCoreData(name: String, entityName: String, key: String)  {
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let entity = NSEntityDescription.entity(forEntityName: entityName, in: context)
    let newEntity = NSManagedObject(entity: entity!, insertInto: context)

    newEntity.setValue(name, forKey: key)

    do {

        try context.save()
        print("saved")

    } catch {
        print("Failed saving")
    }

}


Later in my code I retrieve the data like this:


var dateFromCD = getCoreData(Entity: "EntityDate")

func getCoreData(Entity: String) -> Array<NSManagedObject>{

    var coreData= [NSManagedObject]()
    coreData= []

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: Entity)
    request.returnsObjectsAsFaults = false

    do {
        let result = try context.fetch(request)
        for data in result as! [NSManagedObject]
        {

            coreData.append(data)

        }
    } catch {
        print("Failed")
    }

    return coreData

}



Now I thought I would add a variable time to the seperate attribute "time", and naturally I proceeded by doing like this:

addToCoreData(name: getFormattedTime(), entityName: "EntityDate", key: "time")

The problem I have is that when I call the function "getCoreData(Entity: "EntityDate")" it gives me this output

[<EntityDate: 0x6000034fc2d0> (entity: EntityDate; id: 0xc229f3dcff76efb1 <x-coredata://2E6F1D46-F49D-436C-9608-FEC59EEB21E6/EntityDate/p18>; data: {
    date = "2/21/20";
    time = nil;
}), <EntityDate: 0x6000034fc320> (entity: EntityDate; id: 0xc229f3dcff72efb1 <x-coredata://2E6F1D46-F49D-436C-9608-FEC59EEB21E6/EntityDate/p19>; data: {
    date = nil;
    time = "13:46";
})]

I dont understand the output and I would like to be able to retrieve only date and only time as two seperate variables from the array.

1 Answer 1

1

The problem is that you are creating two records, one with the date and the other with the time information instead of one with both date and time.

If you have only one entity anyway take advantage of the generic behavior and write the method more specific like

func createEntityDate(date: String, time: String)  {
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let newEntity = EntityDate(context: context)
    newEntity.date = date
    newEntity.time = time

    do {
        try context.save()
        print("saved")

    } catch {
        print("Failed saving", error)
    }

}

If you need to modify attributes of an existing record you have to fetch it, modify and save it back.

In any case it's highly recommended to save the whole date as Date and use computed properties to extract the date and time portion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.