ios – ParseSwift Relations At all times Nil, Unable to Save Relations in Parse Backend

ios – ParseSwift Relations At all times Nil, Unable to Save Relations in Parse Backend


I am engaged on an iOS app utilizing the ParseSwift SDK, the place I want to save lots of an Train object that has relations to MuscleGroup (for major and secondary muscle tissues) and Tools. Nevertheless, even after saving the Train object, the relations (primaryMuscles, secondaryMuscles, tools) stay nil and will not be saved within the database.

I outline my mannequin as follows:

struct Train: ParseObject {
    var objectId: String?
    var identify: String
    var isDefault: Bool
    var caloriesPerRep: Double
    var primaryMuscles: ParseRelation?
    var secondaryMuscles: ParseRelation?
    var tools: ParseRelation?
    var picture: ParseFile?
    var consumer: Pointer?

    init(identify: String, isDefault: Bool, caloriesPerRep: Double, picture: ParseFile?) {
        self.identify = identify
        self.isDefault = isDefault
        self.caloriesPerRep = caloriesPerRep
        self.picture = picture
    }
}

My Save Code:

Right here’s how I try to save lots of the Train object and add relations:

@objc func saveButtonTapped() {
    guard let identify = nameTextField.textual content,
          let energy = Double(caloriesTextField.textual content ?? "0"),
          let currentUser = Person.present else {
        print("Error: Person is just not logged in or fields are lacking.")
        return
    }

    let imageFile: ParseFile? = exerciseImage != nil ? ParseFile(identify: "picture.jpg", information: exerciseImage!.jpegData(compressionQuality: 0.8)!) : nil

    // Create a brand new Train object
    var newExercise = Train(identify: identify, isDefault: isDefaultSwitch.isOn, caloriesPerRep: energy, picture: imageFile)

    // Connect the present consumer to the Train object
    do {
        newExercise.consumer = attempt Pointer(currentUser)
    } catch {
        print("Error creating consumer pointer: (error.localizedDescription)")
        return
    }

    // Including relations for major muscle tissues
    if !selectedPrimaryMuscles.isEmpty {
        print("Chosen Major Muscle groups: (selectedPrimaryMuscles)")
        do {
            let primaryMuscleRelation = newExercise.primaryMuscles
            attempt primaryMuscleRelation?.add(selectedPrimaryMuscles) // Add chosen major muscle tissues
        } catch {
            print("Error including major muscle tissues: (error.localizedDescription)")
        }
    }

    // Including relations for secondary muscle tissues
    if !selectedSecondaryMuscles.isEmpty {
        print("Chosen Secondary Muscle groups: (selectedSecondaryMuscles)")
        do {
            let secondaryMuscleRelation = newExercise.secondaryMuscles
            attempt secondaryMuscleRelation?.add(selectedSecondaryMuscles) // Add chosen secondary muscle tissues
        } catch {
            print("Error including secondary muscle tissues: (error.localizedDescription)")
        }
    }

    // Including relations for tools
    if !selectedEquipment.isEmpty {
        print("Chosen Tools: (selectedEquipment)")
        do {
            let equipmentRelation = newExercise.tools
            attempt equipmentRelation?.add(selectedEquipment) // Add chosen tools
        } catch {
            print("Error including tools: (error.localizedDescription)")
        }
    }

    // Save the train to the database
    newExercise.save { end in
        change consequence {
        case .success(let savedExercise):
            print("Train saved: (savedExercise)")
        case .failure(let error):
            print("Error saving Train: (error.localizedDescription)")
        }
    }
}

Downside:

The Train object is saved efficiently, however the relations (primaryMuscles, secondaryMuscles, tools) stay nil within the database.
When I attempt to add objects to those relations utilizing add, they’re nonetheless nil.

What I’ve Tried:

I’ve confirmed that the chosen MuscleGroup and Tools objects are legitimate earlier than attempting so as to add them to the relations.

Query:

Why are my relations (ParseRelation) nonetheless nil. How can I appropriately add and save these relations utilizing ParseSwift?
Any assist can be vastly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *