我创建了一些默认数据,并在首次发布时将其保存在coredata中,但在创建过程中出现问题,我无法弄清楚它是什么。 (如果重要的话,使用sugarRecord)
这些是创作的功能:
默认dataLayout(工作正常)
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")
}
}
使用默认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)")
}
}
&#34;选择的两个版画&#34;给出不同的东西,第一次打印对象是正确的并且设置了dataLayout和settingsLayout,但是在第二次打印时它们已经消失了。我以相同的方式获取和设置目标,并且 保留在对象中。
哪里出错?
目标创建功能用于比较
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")
}
}
以下是我获得的2张照片:
<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;
})
答案 0 :(得分:0)
我忘了将dataLayout和settingsLayout关系设置为很多,修复了它