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 @Relationship
s 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 🌚