I'm creating some default data and saving it in coredata on first launch but during creation something is going wrong and I can't figure out what it is. (Using sugarRecord if that matters)
These are the functions for creation:
Default dataLayout (works fine)
func createDefaultDataLayout() {
guard let db = db else { return }
do {
try db.operation { (context, save) throws -> Void in
let defaultData: TMDataLayout = try! context.create()
defaultData.id = 999
let spot1: TMData = (try! context.fetch(FetchRequest<TMData>().filtered(with: "id", equalTo: "1")).first)!
spot1.spot = 1
let spot2: TMData = (try! context.fetch(FetchRequest<TMData>().filtered(with: "id", equalTo: "6")).first)!
spot2.spot = 2
let spot3: TMData = (try! context.fetch(FetchRequest<TMData>().filtered(with: "id", equalTo: "8")).first)!
spot3.spot = 3
let spot4: TMData = (try! context.fetch(FetchRequest<TMData>().filtered(with: "id", equalTo: "19")).first)!
spot4.spot = 4
defaultData.data = [spot1, spot2, spot3, spot4]
save()
}
} catch {
print("Error creating default data layout")
}
}
Default activities, using the default dataLayout
func createDefaultActivities() {
guard let db = db else { return }
let defaultPredicate: NSPredicate = NSPredicate(format: "id == %@", "0")
do {
try db.operation { (context, save) throws -> Void in
let defaultDataLayout = try! context.fetch(FetchRequest<TMDataLayout>().filtered(with: defaultPredicate)).first
let defaultSettings = try! context.fetch(FetchRequest<TMSettingsLayout>().filtered(with: defaultPredicate)).first
let chosen: TMActivity = try! context.create()
chosen.id = 999
chosen.title = L10n.Activity.running
chosen.iconName = "running"
chosen.dataLayout = defaultDataLayout
chosen.settingsLayout = defaultSettings
chosen.goal = try! context.fetch(FetchRequest<TMGoal>().filtered(with: "title", equalTo: L10n.Goal.Nothing.title)).first
chosen.isPartOfWorkout = false
print(chosen)
let walking: TMActivity = try! context.create()
walking.id = 1
walking.title = L10n.Activity.walking
walking.iconName = "walking"
walking.dataLayout = defaultDataLayout
walking.settingsLayout = defaultSettings
walking.isPartOfWorkout = false
print(chosen)
save()
}
} catch {
print("Something went wrong setting up core data (dataManager)")
}
}
The two prints of "chosen" give something different, the first print the object is correct and has the dataLayout and settingsLayout set, but on the second one they have disappeared. I fetch and set the goal in the same way and that does stay in the object.
Where is it going wrong?
Goal creation func for comparison
func createGoalObjects() {
guard let db = db else { return }
do {
try db.operation { (context, save) throws -> Void in
let duration: TMGoal = try! context.create()
duration.id = 1
duration.title = L10n.Goal.Duration.title
duration.iconName = "duration"
duration.amount = 0
duration.descriptionString = L10n.Goal.Duration.amount(Int(duration.amount))
let pace: TMGoal = try! context.create()
pace.id = 2
pace.title = L10n.Goal.Pace.title
pace.iconName = "pace"
pace.amount = 0
pace.descriptionString = L10n.Goal.Pace.amount(Int(duration.amount))
let distance: TMGoal = try! context.create()
distance.id = 3
distance.title = L10n.Goal.Distance.title
distance.iconName = "distance"
distance.amount = 0
distance.descriptionString = L10n.Goal.Distance.amount(duration.amount)
let calories: TMGoal = try! context.create()
calories.id = 4
calories.title = L10n.Goal.Calories.title
calories.iconName = "calories"
calories.amount = 0
calories.descriptionString = L10n.Goal.Calories.amount(Int(duration.amount))
let nothing: TMGoal = try! context.create()
nothing.id = 5
nothing.title = L10n.Goal.Nothing.title
nothing.iconName = "nothing"
nothing.amount = 0
nothing.descriptionString = L10n.Goal.Nothing.amount
save()
}
} catch {
print("Error creating default data layout")
}
}
Here are the 2 prints I get:
<TMActivity: 0x1c40990a0> (entity: TMActivity; id: 0x1c422d2c0 <x-coredata:///TMActivity/tA820CA64-1F5A-480D-A631-0BC3E05FFC9237> ; data: {
dataLayout = "0xd000000000040002 <x-coredata://62477925-6CCB-472D-8C45-BD6731B59AA6/TMDataLayout/p1>";
goal = "0xd0000000000c0006 <x-coredata://62477925-6CCB-472D-8C45-BD6731B59AA6/TMGoal/p3>";
iconName = running;
id = 999;
isPartOfWorkout = 0;
partId = 0;
parts = (
);
settingsLayout = "0xd000000000040004 <x-coredata://62477925-6CCB-472D-8C45-BD6731B59AA6/TMSettingsLayout/p1>";
title = Hardlopen;
})
<TMActivity: 0x1c40990a0> (entity: TMActivity; id: 0x1c422d2c0 <x-coredata:///TMActivity/tA820CA64-1F5A-480D-A631-0BC3E05FFC9237> ; data: {
dataLayout = nil;
goal = "0xd0000000000c0006 <x-coredata://62477925-6CCB-472D-8C45-BD6731B59AA6/TMGoal/p3>";
iconName = running;
id = 999;
isPartOfWorkout = 0;
partId = 0;
parts = (
);
settingsLayout = nil;
title = Hardlopen;
})
I forgot to set my dataLayout and settingsLayout relationship to many, that fixed it