I have an app similar to Instagram, in it I have to fetch posts and users to go with them. Now I have come to a point where I need to decide between two DM for how to store posts, below are the two options:
option 1: each post has a saves teh user that made it but teh user object is only a user object meaning it might be inefficient as I would have to keep saving teh same data into a User object
class Post: NSObject {
var images = Image()
let user = User()
var snapshot : [String : AnyObject]?
}
option 2: Here the post object would not keep track of teh user as teh user object keeps track of each post
class User: NSObject {
var posts = [Post]()
//Other data...
}
So my question is, which DM is best for my use case (that is to be able to fetch and display posts in a format similar to Instagram)
@ Galo Torres Sevilla comment is the best answer here so I will add it as teh correct answer:
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.
– Galo Torres Sevilla