5.9 C
London
Friday, December 15, 2023

ios – Find out how to add a publish to firestore


I’m attempting to add a textual content string / a picture and a video collectively . Once I add the textual content string and picture collectively they add efficiently however once I add this code to add the video it’s canceled out my textual content string and it’s not importing to firestore however the video is importing

guard let videoUrlString = mediaPreview?.url.absoluteString else { return }
    isLoading = true
    do{
        guard let url = URL(string: videoUrlString) else { return }
        guard let videoUrl = attempt await VideoUploader.uploadVideoToStorage(withUrl: url) else { return }

What’s unsuitable with the code that’s throwing off my code that’s not permitting my textual content string to add ?

That is the code I take advantage of to efficiently add the picture and textual content publish



import SwiftUI
import Firebase
import PhotosUI
import FirebaseFirestoreSwift

@MainActor
class UploadPostViewModel: ObservableObject {
    @Printed var didUploadPost = false
    @Printed var isLoading = false
          @Printed var error: Error?
    @Printed var movies = [Video]()
    @Printed var mediaPreview: Film?
          @Printed var profileImage: Picture?
          @Printed var textual content = ""
          @Printed var selectedImage: PhotosPickerItem? {
              didSet { Job { await loadImage(fromItem: selectedImage)
                  Job { attempt await uploadVideo() }
              } }
          }
          
          non-public var uiImage: UIImage?
   
          func uploadPost(caption: String) async throws {
             
            guard let uid = Auth.auth().currentUser?.uid else { return }
          
                  var imageUrl: String? = nil
                  if let picture = uiImage {
                      imageUrl = attempt await ImageUploader.uploadImage(picture: picture , kind: .publish)
                  }
              
                  
                  let publish = Publish(
                    ownerUid: uid,
                    textual content: textual content, caption: caption,
                     likes: 0,
                    replyCount: 23, imageUrl: imageUrl,
                    timestamp: Timestamp()
                  )
                  
                  attempt await PostService.uploadPost(publish)
                  self.didUploadPost = true
             
              }
              
              
              func loadImage(fromItem merchandise: PhotosPickerItem?) async {
                  guard let merchandise = merchandise else { return }
                  
                  guard let knowledge = attempt? await merchandise.loadTransferable(kind: Knowledge.self) else { return }
                  guard let uiImage = UIImage(knowledge: knowledge) else { return }
                  self.uiImage = uiImage
                  self.profileImage = Picture(uiImage: uiImage)
              }
    func uploadVideo() async throws {
            
        guard let videoUrlString = mediaPreview?.url.absoluteString else { return }
        isLoading = true
        guard let url = URL(string: videoUrlString) else { return }
        guard let videoUrl = attempt await VideoUploader.uploadVideoToStorage(withUrl: url) else { return }
            attempt await Firestore.firestore().assortment("movies").doc().setData(["videoUrl": videoUrl])
        }
        func fetchVideos() async throws {
            let snapshot = attempt await Firestore.firestore().assortment("movies").getDocuments()
            self.movies = snapshot.paperwork.compactMap({ attempt? $0.knowledge(as: Video.self) })
            }
        }

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here