Comments (6)
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.
To understand what you mean, a code snippet could help.
from photostream.
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:
from photostream.
@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.
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.
I think there is way. You can implement by having a queue.
from photostream.
Related Issues (9)
- sign in HOT 4
- The post upload view didn't show the uploadView.progressView HOT 1
- After so many years I still benefit from your photostream VIPER demo.
- iOS11 and Swift 4 Support HOT 3
- Image filters HOT 1
- it is not allowing the to store the post and show in the news feed HOT 13
- Comments editor bar frequently disappear and can not retrieve from database HOT 1
- CommentWriter randomly disappear with no reason. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from photostream.