SwiftUI fetch items on background and display.

Updated
Sep 27, 2019 7:31 PM
Created
Sep 27, 2019 7:31 PM
Tags
SwiftUI.framework
Keywords

struct PhotoDetailView: View {
  
  @EnvironmentObject var store: LoggedInStore
  @State var hasFirstApearBeenDone: Bool = false
  @State var displayComments: [SnapshotFeedPostComment] = []
  
  let post: SnapshotFeedPost
    
  @discardableResult
  private func fetchDisplayComments() -> Future<[SnapshotFeedPostComment], Never> {
    .init { promise in
      
      let comments = self.store.state.normalizedState.comments

      DispatchQueue.global(qos: .userInitiated).async {
        let result = comments
          .filter { $0.value.postID == self.post.id }
          .map { $0.value }
          .sorted { $0.updatedAt > $1.updatedAt }
        
        self.displayComments = result
        
        promise(.success(result))
      }
    }
  }
          
  var body: some View {
            
    return VStack {
      List {
        ForEach(displayComments) { item in
          Text("\(item.body ?? "none")")
        }
      }
    }
    .navigationBarTitle("Comments")
    .navigationBarItems(trailing: HStack {
      Button(action: {
        self.store.addAnyComment(to: self.post)
      }) {
        Text("Add")
      }
    })
      .onAppear {
        
        if !self.hasFirstApearBeenDone {
          self.hasFirstApearBeenDone = true
          self.fetchDisplayComments()
        }
        
    }
    .onDisappear {
      print("disappear")
    }
  }
  
}

Other Notes

Notes