TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SwiftData: improper model with nonsensical relationship definitions

Forums > Swift

I'm not sure how to model basic Twitter with SwiftData, at the beginning I even went to an SQL-like approach with no relationships and a bunch of pivots (which will work, but there is no point in using SwiftData in this case), but decided to try @Relationships and now I'm getting an error from the topic's title. It's more like a beg for help rather than a question, I'd like to understand how to properly model smth like this using SwiftData 🫠:

import SwiftData
import LocalExtensions

@Model
public final class TweetModel: Equatable, Identifiable, Sendable {
    @Attribute(.unique)
    public var id: String
    public var createdAt: Date

    @Relationship(inverse: \UserModel.likedTweets)
    public var author: UserModel

    @Relationship
    public var repostSource: TweetModel?

    @Relationship
    public var replySource: TweetModel?

    @Relationship(inverse: \TweetModel.replySource)
    public var replies: [TweetModel]

    @Relationship(inverse: \TweetModel.repostSource)
    public var reposts: [TweetModel]

    @Relationship(inverse: \UserModel.likedTweets)
    public var likes: [UserModel]

    public var content: String

    public init(
        id: USID,
        createdAt: Date = .now,
        author: UserModel,
        repostSource: TweetModel? = nil,
        replySource: TweetModel? = nil,
        replies: [TweetModel] = [],
        reposts: [TweetModel] = [],
        likes: [UserModel] = [],
        content: String
    ) {
        self.id = id.rawValue
        self.createdAt = createdAt
        self.author = author
        self.repostSource = repostSource
        self.replySource = replySource
        self.replies = replies
        self.reposts = reposts
        self.likes = likes
        self.content = content
    }
}
import SwiftData
import LocalExtensions

@Model
public final class UserModel: Equatable, Identifiable, Sendable {
    @Attribute(.unique)
    public var id: String

    @Attribute(.unique)
    public var username: String

    public var password: Data
    public var displayName: String
    public var bio: String
    public var avatarURL: URL?

    @Relationship(deleteRule: .cascade)
    public var tweets: [TweetModel]

    @Relationship
    public var likedTweets: [TweetModel]

    @Relationship(inverse: \UserModel.followers)
    public var follows: [UserModel]

    @Relationship
    public var followers: [UserModel]

    public init(
        id: USID,
        username: String,
        password: Data,
        displayName: String = "",
        bio: String = "",
        avatarURL: URL? = nil,
        tweets: [TweetModel] = [],
        likedTweets: [TweetModel] = [],
        follows: [UserModel] = [],
        followers: [UserModel] = []
    ) {
        self.id = id.rawValue
        self.username = username
        self.password = password
        self.displayName = displayName
        self.bio = bio
        self.avatarURL = avatarURL
        self.tweets = tweets
        self.likedTweets = likedTweets
        self.follows = follows
        self.followers = followers
    }
}

Here is a project you can clone and run to get improper model with nonsensical relationship definitions crash 🌚

2      

Well, after writing this post I noticed that I was trying to connect TweetModel.author with UserModel.likedTweets instead of UserModel.tweets

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.