Core Data using NSArray does not preserve any data after program termination

37 Views Asked by At

I'm attempting to configure the transformer to preserve array objects. It was successful to create new data after refreshing data, however when I killed the program and then reopened it, the data was gone. What did I overlook?

Here my startup code:

func createDatabase() {
    ValueTransformer.setValueTransformer(UUIDTransformer(), forName: NSValueTransformerName("UUIDTransformer"))
    ValueTransformer.setValueTransformer(TheCategoriesTransformer(), forName: NSValueTransformerName("TheCategoriesTransformer"))
    
    do { theDailyData = try contextDataCore.fetch(The_daily_data.fetchRequest()) } catch { }
    do { theHabitList = try contextDataCore.fetch(The_habit_list.fetchRequest()) } catch { }
    do { thePhotoData = try contextDataCore.fetch(The_photos.fetchRequest()) } catch { }
}

Here my transformer code:

class UUIDTransformer: ValueTransformer {
    override func transformedValue(_ value: Any?) -> Any? {
        guard let uuid = value as? [UUID] else { return nil }

        do {
            let data = try NSKeyedArchiver.archivedData(withRootObject: uuid, requiringSecureCoding: true)
            return data
        } catch {
            return nil
        }

    }

    override func reverseTransformedValue(_ value: Any?) -> Any? {
        guard let data = value as? Data else { return nil }

        do {
            let uuid = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: data) as! [UUID]
            return uuid
        } catch {
            return nil
        }

    }
}

class TheCategoriesTransformer: ValueTransformer {
    override func transformedValue(_ value: Any?) -> Any? {
        guard let uuid = value as? [String] else { return nil }

        do {
            let data = try NSKeyedArchiver.archivedData(withRootObject: uuid, requiringSecureCoding: true)
            return data
        } catch {
            return nil
        }

    }

    override func reverseTransformedValue(_ value: Any?) -> Any? {
        guard let data = value as? Data else { return nil }

        do {
            let categories = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: data) as! [String]
            return categories
        } catch {
            return nil
        }

    }
}

Create new data:

func newJournalData(theDate: Date, theHabitID: UUID? = nil, theSubject: String? = nil, theJournal: String? = nil, theMind: String? = nil, theWeather: String = "NA") {
    let repairDate = Calendar.current.dateComponents([.year, .month, .day], from: theDate)
    let freshDate = Calendar.current.date(from: repairDate)
    
    var newHabitID: [UUID] = []
    if theHabitID != nil { newHabitID.append(theHabitID!) }
    
    var theImpactCount = 0
    var theImpactTotal = 0
    
    let findHabitImpact = theHabitList.first(where: { $0.theID == theHabitID })
    
    for countX in theHabitList {
        switch countX.impact {
        case 0: theImpactTotal += 1
        case 1: theImpactTotal += 5
        case 2: theImpactTotal += 10
        default: theImpactTotal += 0
        }
    }
    
    if findHabitImpact != nil {
        switch findHabitImpact?.impact {
        case 0: theImpactCount += 1
        case 1: theImpactCount += 5
        case 2: theImpactCount += 10
        default: theImpactCount += 0
        }
    }
    
    let finalImpact = Int((Double(theImpactCount) / Double(theImpactTotal)) * 100)
    
    let newJournalData = The_daily_data(context: contextDataCore)
    newJournalData.date = freshDate
    newJournalData.habitData = newHabitID
    newJournalData.theSubject = theSubject
    newJournalData.journal = theJournal
    newJournalData.yourMind = theMind
    newJournalData.theWeather = theWeather
    newJournalData.percent = Int16(finalImpact)
    
    do { try contextDataCore.save(); createDatabase() } catch { print("Error") }
}

Screenshot:

enter image description here enter image description here

I can see the array from habitData without any problems when I create the data. When the program is closed and reopened, just the habitData data is lost; all other data remains intact. After restarting the program, habitData always returns nil.

0

There are 0 best solutions below