TheNoviceProgrammer
TheNoviceProgrammer

Reputation: 73

Parse/SwiftUI - PFObject values may not have class: PFRelation

Everytime I try to decode the data, the application crashes with:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'PFObject values may not have class: PFRelation'

This is my model:

class ParseUserPosts: PFObject {
    @NSManaged var userId: String
    @NSManaged var posts: PFRelation<ParsePostModel>
}

class ParsePostModel: PFObject {
    @NSManaged var imageURL: String
    @NSManaged var userId: String
    @NSManaged var caption: String
}

And this is function which decodes:

func decodeUserPostsObject(item: PFObject, onSuccess: @escaping(_ returnedItem: ParseUserPosts) -> Void) {
    var userPost = ParseUserPosts(className: "UserPosts")
    //MARK: Decode post details
    userPost.userId = item["userId"] as! String
    userPost.posts = item["posts"] as! PFRelation
     onSuccess(userPost)
}

The line that is giving me the issue is:

userPost.posts = item["posts"] as! PFRelation

I've tried:

userPost.posts = item["posts"] as! PFRelation<ParseUserPosts>

However, still no luck. I am using Back4App and in the console the relations are created/updated with no issue at all, however, it's when I try to decode the data into my application the crash occurs.

If anyone could give me an indication as to what I am doing wrong, would be much appreciated.

Upvotes: 0

Views: 35

Answers (1)

TheNoviceProgrammer
TheNoviceProgrammer

Reputation: 73

The issue was this line of code:

 @NSManaged var posts: PFRelation<ParsePostModel>

Simply by declaring an array of PFObjects, it was fine.

 @NSManaged var posts: [PFObjects]

And to decode the data, perform a query as such:

var query = obj.relation(forKey: "posts").query()
  query.findObjectsInBackground { allPosts, error in
  //MARK: Assign your relation data how you see fit
}

Upvotes: 0

Related Questions