Giter Site home page Giter Site logo

Comments (6)

mownier avatar mownier commented on May 14, 2024

I did not get what you actually mean. But AFAIK, there is no observer added every post. What is happening when a user follows is that, all the posts of the followed user will be copied/put into the feed of the logged in user.

from photostream.

mownier avatar mownier commented on May 14, 2024

To understand what you mean, a code snippet could help.

from photostream.

MHX792 avatar MHX792 commented on May 14, 2024

In PostServiceProdiver.swift you have written:

func fetchPosts(userId: String, offset: String, limit: UInt, callback: ((PostServiceResult) -> Void)?) {
    var result = PostServiceResult()
    guard session.isValid else {
        result.error = .authenticationNotFound(message: "Authentication not found")
        callback?(result)
        return
    }
    
    let uid = session.user.id
    let rootRef = FIRDatabase.database().reference()
    let usersRef = rootRef.child("users")
    let postsRef = rootRef.child("posts")
    let photosRef = rootRef.child("photos")
    let userPostRef = rootRef.child("user-post/\(userId)/posts")
    var query = userPostRef.queryOrderedByKey()
    
    if !offset.isEmpty {
        query = query.queryEnding(atValue: offset)
    }
    
    query = query.queryLimited(toLast: limit + 1)
    query.observeSingleEvent(of: .value, with: { (data) in
        guard data.childrenCount > 0 else {
            result.posts = PostList()
            callback?(result)
            return
        }
        
        var posts = [Post]()
        var users = [String: User]()
        
        for child in data.children {
            guard let userPost = child as? FIRDataSnapshot else {
                continue
            }
            
            let postId = userPost.key
            let postRef = postsRef.child(postId)
            
            postRef.observeSingleEvent(of: .value, with: { (postSnapshot) in
                guard let posterId = postSnapshot.childSnapshot(forPath: "uid").value as? String,
                    let photoId = postSnapshot.childSnapshot(forPath: "photo_id").value as? String else {
                        return
                }
                
                let userRef = usersRef.child(posterId)
                let photoRef = photosRef.child(photoId)
                let likesRef = rootRef.child("post-like/\(postId)/likes")
                
                userRef.observeSingleEvent(of: .value, with: { (userSnapshot) in
                    photoRef.observeSingleEvent(of: .value, with: { (photoSnapshot) in
                        likesRef.observeSingleEvent(of: .value, with: { (likesSnapshot) in
                        
                            //...
                        })
                    })
                })
            })
        }
    })
}

You are chaining observers. For every post you have 3 additional observers. If there are 100 posts in your stream, this means that you will have at least 400 observers.

Isn't this too much?

See this related SO question:

http://stackoverflow.com/questions/41558394/firebase-best-way-for-observing-post-metadata-in-a-social-network

from photostream.

mownier avatar mownier commented on May 14, 2024

@MHX792 Yep, I already got your point. This function will produce a large amount of observers. I guess the implementation of post pagination using the offset and limit parameters makes the function effective. If you just limit your fetch up to 10, you have only 40 observers per fetch. I think that is fair enough.

from photostream.

MHX792 avatar MHX792 commented on May 14, 2024

But fetching only 10 posts is way to insufficient. It should show at least the latest 50 posts which would result in having 200 observers. Isn't there any other way to do this?
How about embedding the likes and other data in the posts?
The downside would be that likes need to be updated in different nodes when somebody (un-)likes a post.

from photostream.

mownier avatar mownier commented on May 14, 2024

I think there is way. You can implement by having a queue.

from photostream.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.