Found a circular dependency when encoding

I for the life of me cannot figure out what’s triggering this error. I have a button that saves a usersProfile

var tempProfile = usersProfile
tempProfile?.description = aboutText
 tempProfile?.smokes = smoking
tempProfile?.ethnicityCode = ethnicity

tempProfile?.save { result in
      switch result {
            case .success(let profile):
            usersProfile = profile
            showQualifyingQuestions.toggle()
            case .failure(let error):
            print(error.message)
            }
     }
                                                        
}

sometimes Ill get that error message, other times it works. If I get that error twice in a row the app will crash.

Fatal error: Duplicate elements of type ‘ParseFile’ were found in a Set.

This usually means either that the type violates Hashable’s requirements, or

that members of such a set were mutated after insertion.

I cannot figure out why randomly its fine and other times its not.

ParseSwift version: 2.5.1
Parse Server Version: 4.4

Corey might correct me, but I think that the usersProfile = profile in the success result is what cause the duplicate because success itself saves User.current to keychain automatically?

In this particular case the usersProfile is their public profile which is saved separate (separate object) from the User. usersProfile is a type ParseObject not a ParseUser. However I was also saving the actual User (I removed it for clarity). I will test removing the currentUser = user and come back.

No luck, same result (randomly)

@EliteTechnicalCare you should provide more information when you ask these types questions. For example, show all of your complete ParseObject 's with their respective properties…

I am not sure how this would be beneficial but here:

@cbaker6

struct PublicProfile: ParseObject, ParseObjectMutable {
    //: Required Variables
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    
    var name: String?
    var dob: Date?
    var religion: Int?
    var gender: Int?
    var location: ParseGeoPoint?
    
    var verificationPicture: ParseFile?
    var defaultProfilePicture: ParseFile?
    var photos: [ParseFile]?
    var description: String?
    
    var smokes: Bool?
    
    var FCMToken: String? //Firebase MEssaging Notifications
    
    var approved: Bool?
    var disapprovalReason: String?
    
    var ethnicityCode: String?
    
    
    
    
    static func sampleUser() -> PublicProfile {
        let publicProfile = PublicProfile(objectId: "a", name: "Testing Spouse", dob: Date(), gender: 0, description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", smokes: true)
        
        return publicProfile
    }
}

Based on your error:

and the fact that a ParseObject needs to be Hashable, I’m guessing your problem is with your ParseObject, specifically, var photos: [ParseFile]?. Maybe it contains two of the same photos? You can try to change it to, Set<ParseFile>? instead of an array to try to maintain uniqueness.

The error seems to be coming from Swift and not the SDK, so it seems you are doing something weird during runtime. From searching online, that error using comes from reference types (classes), which from the code you’ve shown isn’t being used. You can try to only save an immutable version:

var tempProfile = usersProfile
tempProfile?.description = aboutText
 tempProfile?.smokes = smoking
tempProfile?.ethnicityCode = ethnicity

let savableProfile = tempProfile
savableProfile?.save { result in
      switch result {
            case .success(let profile):
            usersProfile = profile
            showQualifyingQuestions.toggle()
            case .failure(let error):
            print(error.message)
            }
     }                                                 
}

You can also try to save all of your ParseFiles before saving your profile to see if that removes the error.

1 Like

I updated the photos to a set and it works perfectly now. Thank you so much I’ve been pulling my hair at this issue. Question is there any benefit or reason to save an immutable version?

The mutable version should work fine (particularly since you said your error went away), but since your error was saying something was being modified when it wasn’t suppose to, the immutable part prevents modification.