UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

public final class TweetModel: Equatable, Identifiable, Sendable {
    public var id: String
    public var createdAt: Date

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

    public var repostSource: TweetModel?

    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

public final class UserModel: Equatable, Identifiable, Sendable {
    public var id: String

    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]

    public var likedTweets: [TweetModel]

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

    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 🌚


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


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.