Home > Software engineering >  Data unable to save into database IOS CoreData Swiftui Swift
Data unable to save into database IOS CoreData Swiftui Swift

Time:09-12

Hi I have this function that will save data to the entity

 func addOrder(room: Room, userID: String, orderDetail: OrderDetail) {
        let order = Order(context: container.viewContext)
        
        order.id = UUID()
        order.name = orderDetail.name
        order.ownerID = room.ownerID
        order.tenant_id = userID
        order.tenant_ic = orderDetail.identityCard
        order.phoneNum = orderDetail.mobileNumber
        order.move_in_date = DateFormatter().string(from: orderDetail.moveInDate)
        order.created_at = Date()
        order.updated_at = Date()
        order.room_id = room.id!.uuidString
        
        print(order)
        
        save(context: container.viewContext)
    }

however, at the print(order) line this is the output I get

<Order: 0x600000955450> (entity: Order; id: 0x600002aaec60 x-coredata:///Order/tD85A0123-DDC2-48FB-8414-F4F52913C8258; data: {
    "created_at" = "2022-09-11 13:26:18  0000";
    id = "458AF674-BE42-4266-8D3C-6BEA6B78FCAF";
    "move_in_date" = "";
    name = "";
    ownerID = "A0051C62-4C91-4AF3-B42C-79FFD2B7275D";
    phoneNum = "";
    "room_id" = "FBAABEF2-7A58-4BE7-A01A-AA6DECB76767";
    "tenant_ic" = "";
    "tenant_id" = "F7176157-A432-4F0B-9767-BFC09D5AA756";
    "updated_at" = "2022-09-11 13:26:18  0000";
})

why the room_id, tenant_ic.... the keys are double quoted? and I notice those double quoted data, none of them get saved into database why is it so?

Data Model:

import Foundation
import CoreData


extension Order {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Order> {
        return NSFetchRequest<Order>(entityName: "Order")
    }

    @NSManaged public var created_at: Date?
    @NSManaged public var id: UUID?
    @NSManaged public var move_in_date: String?
    @NSManaged public var name: String?
    @NSManaged public var ownerID: String?
    @NSManaged public var phoneNum: String?
    @NSManaged public var room_id: String?
    @NSManaged public var tenant_ic: String?
    @NSManaged public var tenant_id: String?
    @NSManaged public var updated_at: Date?

}

extension Order : Identifiable {

}

CodePudding user response:

I noticed that the only ones double quoted had underscores (_) in them.

Why don’t you try using camelCase case instead?

  • Related