'Upload videoUrl to firebase database in switch

I'm trying to make a simple Instagram Clone App to learn more about Firebase. So far I can share Images, Comments, Likes, but when it comes to videos, the app fails to do that. I mange to upload the Videofile to the Storage, but I don't get the videoUrl to the Database. I'm grateful for suggestions.

class ShareService {
    static var REF_STORAGE_POST = Storage.storage().reference().child("posts")
    
    static func uploadDataToStorage(imageData: Data, videoUrl: URL? = nil, postText: String, imageRatio: CGFloat, onSuccess: @escaping () -> Void ) {
        
        // 1. upload Video
        if let _videoUrl = videoUrl {
            self.uploadVideoToFireBaseStorage(videoUrl: _videoUrl, onSucces: { (videoUrlString) in
                self.uploadImageToFireBaseStorage(data: imageData, onSucces: { (thumbnailUrlString) in
                    self.uploadDataToDatabase(imageUrl: thumbnailUrlString, videoUrl: videoUrlString, postText: postText, imageRatio: imageRatio, onSuccess: onSuccess)
                })
            })
        } else {
             // 2. upload image
            self.uploadImageToFireBaseStorage(data: imageData, onSucces: { (imageUrlString) in
                self.uploadDataToDatabase(imageUrl: imageUrlString, postText: postText, imageRatio: imageRatio, onSuccess: onSuccess)
            })            
        }
    }
    
    fileprivate static func uploadVideoToFireBaseStorage(videoUrl: URL, onSucces: @escaping (_ videoUrl: String) -> Void) {

        let videoIdString = NSUUID().uuidString
        let storageRef = REF_STORAGE_POST.child(videoIdString)
        
        print(videoUrl)
        
        let metadata = StorageMetadata()
        //specify MIME type
        metadata.contentType = "video/quicktime"
        
        if let videoUrl = NSData(contentsOf: videoUrl) as Data? {
        storageRef.putData(videoUrl, metadata: metadata) { (metaData, error) in
            if error != nil {
                print("upload video error")
            }
        }
            storageRef.downloadURL(completion: { (url, error) in
                if error != nil {
                    print(error!.localizedDescription)
                    return
                }
                
                let videoUrlString = url?.absoluteString
                onSucces(videoUrlString ?? "no video")
            })
        }
    }
    
    // upload image
    fileprivate static func uploadImageToFireBaseStorage(data: Data,  onSucces: @escaping (_ imageUrl: String) -> Void) {
        let photoIdString = NSUUID().uuidString
        
        let storageRef = REF_STORAGE_POST.child(photoIdString)
        
        storageRef.putData(data, metadata: nil) { (metaData, error) in
            if error != nil {
                print("upload image error")
            }
            storageRef.downloadURL(completion: { (url, error) in
                if error != nil {
                    print(error!.localizedDescription)
                    return
                }
                
                let imageUrlString = url?.absoluteString
                onSucces(imageUrlString ?? "no image")
            })
        }
    }
    

    fileprivate static func uploadDataToDatabase(imageUrl: String, videoUrl: String? = nil, postText: String, imageRatio: CGFloat, onSuccess: @escaping () -> Void) {
        let newPostId = PostApi.shared.REF_POSTS.childByAutoId().key
        let newPostRef = PostApi.shared.REF_POSTS.child(newPostId!)
        
        let postTime = Date().timeIntervalSince1970
        print(postTime)
        
        
        guard let currentUserUid = UserApi.shared.CURRENT_USER_UID else { return }
        var dic = ["uid" : currentUserUid, "imageUrl" : imageUrl, "imageRatio" : imageRatio, "postText" : postText] as [String : Any]
        
        if let _videoUrl = videoUrl {
            dic["videoUrl"] = _videoUrl
        }
        
        newPostRef.setValue(dic) { (error, _) in
            if error != nil {
                print(error?.localizedDescription as Any)
            }
            
            // Show posts in newsfeed
            FeedApi.shared.REF_NEWSFEED.child(currentUserUid).child(newPostId!).setValue(true)
            
            // Eigene Posts markieren
            PostApi.shared.REF_MY_POSTS.child(currentUserUid).child(newPostId!).setValue(true)
            
            print("Post created")
            onSuccess()
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source