Feed & Timeline
Let your users showcase their unique personality right in their timelines
Feed is a great way for users to get relevant updates, communicate and express themselves via algorithmically ranked content.
Each feed consists of a collection of posts. Users will be able to create posts on any groups that they are a member of and any user that they can find.
There are 3 types of feeds:
  1. 1.
    User Feed This is a collection of posts for a user's timeline.
  2. 2.
    Group Feed This is a collection of posts from members of the group or community.
  3. 3.
    Global Feed This is an aggregate of both User and Group feeds.
Feed management methods are all contained in AmityFeedRepository class.
1
let feedRepository = AmityFeedRepository(client: client)
Copied!

Query User Feed

Querying for feed fetches your posts in your own feed or other user's feed. You can also sort the posts by lastCreated or firstCreated timestamp. AmityUserFeedSortOption enum provides both sort options which you can provide as argument while fetching feed.
AmityFeedRepository class exposes two convenient methods getMyFeedSortedBy(_:) and getUserFeed(_:) to query for your own feed and other user's feed respectively. getUserFeed(_:) method requires an additional parameter userId.
1
let feedToken: AmityNotificationToken?
2
3
...
4
5
let feedCollection = feedRepository.getUserFeed(userId, sortBy: .lastUpdated, includeDeleted: false)
6
feedToken = feedCollection.observe({ (collection, _, error) in
7
// React to changes here
8
})
Copied!
There is a quick easy method to get your own feed:
1
let feedCollection = feedRepository.getMyFeedSorted(by: .lastUpdated, includeDeleted: false)
2
feedToken = feedCollection.observe({ (collection, _, error) in
3
// React to changes here
4
})
Copied!
Live Collection for feed provides a maximum of 20 posts per page. You can easily fetch more posts by using nextPage() method from the same live collection.
1
if feedCollection.hasNext {
2
// Fetches more posts from server and appends it to the same collection
3
feedCollection.nextPage()
4
}
Copied!

Query Group Feed

You can get any group's feed by calling the method below with the communityId. Fetching more posts for Group Feed is the same as User Feed.
1
let feedCollection = feedRepository.getCommunityFeed(withCommunityId: "community-id", sortBy: .lastCreated, includeDeleted: false)
2
3
feedToken = feedCollection.observe({(collection, change, error) in
4
// Observer is called once object is received
5
})
Copied!

Query Global Feed

You can retrieve your global feed by calling the following method.
1
let feedCollection = feedRepo.getGlobalFeed()
2
3
feedToken = feedCollection.observe({(collection, change, error) in
4
// Observer is called once object is received
5
})
Copied!
Fetching more posts in global feed is the same as User Feed.

Implementing feed pagination

Whether you're querying a user, group, or global feed, Live Collection will return a maximum of 20 posts in each page. To implement feed pagination:
1
Class ViewController: UIViewController {
2
.
3
.
4
.
5
let feedRepository= AmityFeedRepository(client: client) // client is AmityClient
6
var postCollection: AmityCollection<AmityPost>?
7
var feedCollectionToken: AmityNotificationToken?
8
postCollection = feedRepository.getGlobalFeed()
9
feedCollectionToken = postCollection?.observe({(collection, _, error) in
10
// do something
11
})
12
13
// You can get pagination to load more below
14
15
guard let hasMorePosts = postCollection?.hasNext, hasMorePosts else { return }
16
postCollection?.nextPage()
17
.
18
.
19
.
20
}
Copied!
Last modified 2mo ago