哪种数据模型最适合存储类似 Instagram 的数据?
Which Data Model is the best for storing instagram like data?
我有一个类似于 Instagram 的应用程序,我必须在其中获取 posts 和用户才能使用它们。现在我已经到了需要在两个 DM 之间决定如何存储 posts 的地步,下面是两个选项:
选项 1:每个 post 都有一个保存它的用户,但用户对象只是一个用户对象,这意味着它可能像我那样效率低下继续将相同的数据保存到用户对象中
class Post: NSObject {
var images = Image()
let user = User()
var snapshot : [String : AnyObject]?
}
选项 2:此处 post 对象不会跟踪用户,因为用户对象会跟踪每个 post
class User: NSObject {
var posts = [Post]()
//Other data...
}
所以我的问题是,哪个 DM 最适合我的用例(即能够以类似于 Instagram 的格式获取和显示 posts)
你应该使用
class Post {
var images: [String]
let userId: String
var snapshot : [String : AnyObject]?
}
class User {
let id: String
}
当你获取帖子时,你可以这样做。 repo.findPostsByUserId(userId, page, size)
@Galo Torres Sevilla 评论是这里的最佳答案所以我将其添加为正确答案:
First option is better. Each post should always keep a reference to
who posted it. Users on the other hand don’t need to have a reference
to each of their posts. Imagine a situation where you only want to
display the user’s name and profile pic, why would you fetch all the
posts in that case? Also, the model in your case should not contain
variables that point to reference types like images, it’s better to
keep them in value types such a String that points to the image url
and download the image as needed.
– 加洛托雷斯塞维利亚
我有一个类似于 Instagram 的应用程序,我必须在其中获取 posts 和用户才能使用它们。现在我已经到了需要在两个 DM 之间决定如何存储 posts 的地步,下面是两个选项:
选项 1:每个 post 都有一个保存它的用户,但用户对象只是一个用户对象,这意味着它可能像我那样效率低下继续将相同的数据保存到用户对象中
class Post: NSObject {
var images = Image()
let user = User()
var snapshot : [String : AnyObject]?
}
选项 2:此处 post 对象不会跟踪用户,因为用户对象会跟踪每个 post
class User: NSObject {
var posts = [Post]()
//Other data...
}
所以我的问题是,哪个 DM 最适合我的用例(即能够以类似于 Instagram 的格式获取和显示 posts)
你应该使用
class Post {
var images: [String]
let userId: String
var snapshot : [String : AnyObject]?
}
class User {
let id: String
}
当你获取帖子时,你可以这样做。 repo.findPostsByUserId(userId, page, size)
@Galo Torres Sevilla 评论是这里的最佳答案所以我将其添加为正确答案:
First option is better. Each post should always keep a reference to who posted it. Users on the other hand don’t need to have a reference to each of their posts. Imagine a situation where you only want to display the user’s name and profile pic, why would you fetch all the posts in that case? Also, the model in your case should not contain variables that point to reference types like images, it’s better to keep them in value types such a String that points to the image url and download the image as needed.
– 加洛托雷斯塞维利亚