我正在使用 ParseSwift SDK 开发一个 iOS 应用程序,我需要保存一个与 MuscleGroup(针对主要和次要肌肉)和 Equipment 相关的锻炼对象。然而,即使在保存锻炼对象后,关系(primaryMuscles、 secondaryMuscles、设备)仍然为零并且不会保存在数据库中。
我定义我的模型如下:
struct Exercise: ParseObject {
var objectId: String?
var name: String
var isDefault: Bool
var caloriesPerRep: Double
var primaryMuscles: ParseRelation<MuscleGroup>?
var secondaryMuscles: ParseRelation<MuscleGroup>?
var equipment: ParseRelation<Equipment>?
var image: ParseFile?
var user: Pointer<User>?
init(name: String, isDefault: Bool, caloriesPerRep: Double, image: ParseFile?) {
self.name = name
self.isDefault = isDefault
self.caloriesPerRep = caloriesPerRep
self.image = image
}
}
我的保存代码:
以下是我尝试保存练习对象并添加关系的方法:
@objc func saveButtonTapped() {
guard let name = nameTextField.text,
let calories = Double(caloriesTextField.text ?? "0"),
let currentUser = User.current else {
print("Error: User is not logged in or fields are missing.")
return
}
let imageFile: ParseFile? = exerciseImage != nil ? ParseFile(name: "image.jpg", data: exerciseImage!.jpegData(compressionQuality: 0.8)!) : nil
// Create a new Exercise object
var newExercise = Exercise(name: name, isDefault: isDefaultSwitch.isOn, caloriesPerRep: calories, image: imageFile)
// Attach the current user to the Exercise object
do {
newExercise.user = try Pointer(currentUser)
} catch {
print("Error creating user pointer: \(error.localizedDescription)")
return
}
// Adding relations for primary muscles
if !selectedPrimaryMuscles.isEmpty {
print("Selected Primary Muscles: \(selectedPrimaryMuscles)")
do {
let primaryMuscleRelation = newExercise.primaryMuscles
try primaryMuscleRelation?.add(selectedPrimaryMuscles) // Add selected primary muscles
} catch {
print("Error adding primary muscles: \(error.localizedDescription)")
}
}
// Adding relations for secondary muscles
if !selectedSecondaryMuscles.isEmpty {
print("Selected Secondary Muscles: \(selectedSecondaryMuscles)")
do {
let secondaryMuscleRelation = newExercise.secondaryMuscles
try secondaryMuscleRelation?.add(selectedSecondaryMuscles) // Add selected secondary muscles
} catch {
print("Error adding secondary muscles: \(error.localizedDescription)")
}
}
// Adding relations for equipment
if !selectedEquipment.isEmpty {
print("Selected Equipment: \(selectedEquipment)")
do {
let equipmentRelation = newExercise.equipment
try equipmentRelation?.add(selectedEquipment) // Add selected equipment
} catch {
print("Error adding equipment: \(error.localizedDescription)")
}
}
// Save the exercise to the database
newExercise.save { result in
switch result {
case .success(let savedExercise):
print("Exercise saved: \(savedExercise)")
case .failure(let error):
print("Error saving Exercise: \(error.localizedDescription)")
}
}
}
问题:
Exercise
对象已成功保存,但关系(primaryMuscles
、secondaryMuscles
、equipment
)仍保留在数据库中。
当我尝试使用 add 将对象添加到这些关系中时,它们仍然为零。我尝试过的:在尝试将选定的 MuscleGroup 和 Equipment 对象添加到关系之前,我已确认它们有效。
问题:为什么我的关系(ParseRelation)仍然为零。如何使用 ParseSwift 正确添加和保存这些关系? 任何帮助将不胜感激!
您有几个选择:
选项1:在练习结构中,更改
nil
到
var user: Pointer<User>?
然后用户可以添加到这样的练习中
var user: User?
以上之所以有效,是因为未保存的当前用户对象是与练习对象一起写入的。
选项2:
保留结构体的原样,首先保存 CurrentUser 对象,然后将其添加为练习的指针,或者使用保存的 currentUser 修改已保存的练习。类似的东西
newExercise.user = currentUser