'Binding ObservedObject Init to ContentView
I'm trying to pull more content from server when the user comes to end of the page. I start with loading 5 posts and then 5+5=10 posts. But I cannot bind range so that the view updates with new numbers. Is there a better approach to this or what am I doing wrong here? I tried many things from similiar questions but here is the code:
struct ContentView: View {
@ObservedObject var feedPosts : PostArrayObject
@State var rangeEnd: Int
init(){
let state = State(initialValue: 1)
self._rangeEnd = state
self.feedPosts = PostArrayObject(personalFeed: true, chunkSize: state.projectedValue)
}
var body: some View {
TabView {
NavigationView {
FeedView(posts: feedPosts, title: "Feed", rangeEnd: $rangeEnd)
.toolbar {
....
class PostArrayObject: ObservableObject {
@Published var range: Range<Int> = 0..<1
init(personalFeed: Bool, chunkSize: Binding<Int>) {
DataService.instance.downloadPostsForFollowingInChunks(chunkSize: chunkSize.wrappedValue) { (returnedPosts) in
let shuffledPosts = returnedPosts.shuffled()
self.dataArray.append(contentsOf: shuffledPosts)
self.postsLoaded = true
}
}
struct FeedView: View {
@ObservedObject var posts: PostArrayObject
var title: String
var chunkSize: Int = 5
@Binding var rangeEnd: Int
....
func loadMore(){
rangeEnd = chunkSize
posts.range = 0..<posts.range.upperBound + rangeEnd
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
