Problem with "recursive" ParseRelation with ParseSwift

My User object has a field of “blockedUsers” which in the ParseDatabase is a relation to _User.

Unfortunately, I’m getting an error of

Value type ‘User’ cannot have a stored property that recursively contains it

my User object:

struct User: ParseUser {

//: These are required for `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?

//: These are required for `ParseUser`.
var username: String?
var email: String?
var emailVerified: Bool?
var password: String?
var authData: [String: [String: String]?]?

//: Your custom keys.
var role: Role<User>
var userProfile: UserProfile?
var userSettings: UserSettings?
var blockedUsers: ParseRelation<User> [ERROR HERE]
}

Is there a way around this or should I go store the user objectId in an array instead?

This is same issue your brought up previously just with a different type:

You can’t do what you are trying with a struct, your objectId may work or the Pointer example I gave, it matters your usecase

Hi,

Yes, exactly the same problem as before.

Unfortunately a Pointer might not work as I do need multiple objects, thus a ParseRelation.

Seeing as this isn’t possible with a struct and ParseRelation, I would need to rethink my structure.

thanks @cbaker6

You can probably use the relation property from an object. Look at the documentation for more info.

Hi, thanks for the lead,

I’m still confused on how I would query the data from this relation

let relation = User.current!.relation

if my key is blockedUsers. and I’ve added the user via

relation.add("blockedUsers", UserObject)

How would I actually get the relation later on when I need to find the blockedUsers?

The examples on the documentation link you sent is all about ParseRoles and it kinda confuses me.

Do I

do {
try relation.query().find { result in
switch result {
case .success(let relatedUsers):
print(“success”)
case .failure(let error):
print(“fail”)
}
}
} catch {
print(error)
}

Basically I don’t see where I can set that I want the relation of the currentUser’s blockedUsers when I query.

Appreciate the help.

ParseRole is a ParseRelation. Try looking at how ParsRole is setup as a relation:

1 Like