gpt4 book ai didi

ios - CoreData 对象属性在创建下一个对象时消失

转载 作者:行者123 更新时间:2023-11-30 11:57:18 27 4
gpt4 key购买 nike

我正在创建一些默认数据并在首次启动时将其保存在 coredata 中,但在创建过程中出现了问题,我无法弄清楚它是什么。 (如果重要的话,使用 SugarRecord)

这些是创建函数:

默认数据布局(工作正常)

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")
}
}

默认的activity,使用默认的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)")
}
}

“chosen”的两个打印给出了不同的内容,第一个打印对象是正确的并且设置了 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;
})

最佳答案

我忘记将 dataLayout 和 settingsLayout 关系设置为很多,这解决了它

关于ios - CoreData 对象属性在创建下一个对象时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47658415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com