0

I am trying to learn firebase.I have two folders on firebase

1)VDBackgroundFrames/ 2)VDFrames/

In both folders ,we have 4 images - VDBG2.png,VDBG3.png,VDBG4.png,VDBG5.png.

I am able to access one image at a time from firebase using the following code:-

 func firebaseSetUp(){

        let store = Storage.storage()
        let storeRef = store.reference()
        let userProfilesRef = storeRef.child("VDBackgroundFrames/VDBG11.jpg")
        userProfilesRef.downloadURL { (url,error) in
            if error != nil {
                print("error?.localizedDescription",error?.localizedDescription)
                return
            }else{
                print("url",url!)
            }
        }
    }

//==========updated code ====//

func firebaseSetUp(){

            let store = Storage.storage()
            let storeRef = store.reference()
            let userProfilesRef = storeRef.child("VDBackgroundFrames/")
            userProfilesRef.observe(.childAdded, with: { [weak self] (snapshot) -> Void in
                guard let strongSelf = self else { return }
                //Logic to extract urls...

                }, changeHandler: (StorageReference, NSKeyValueObservedChange<Value>) -> Void)

    }

Output that I am obtaining is as follows:-

URL

https://firebasestorage.googleapis.com/v0/b/celebrations-8edf8.appspot.com/o/VDBackgroundFrames%2FVDBG11.jpg?alt=media&token=ae0910d1-2139-4443-b19a-02edde2f9b17

I actually want to access all the 4 images together from folder VDBackgroundFrames & VDFrames respectively.Kindly suggest the possible way to do it.Any suggestion or guidance would be apprecialble. Thanks in advance.

Folders On Firebase

Images in folder VDBackgroundFrames

Prez
  • 227
  • 3
  • 12
  • You need to download one file at a time. You can only zip them together. – Leo Dabus Jan 24 '19 at 17:30
  • It's unclear why they need to be downloaded 'together'. The download would generally download one file, then download another or you can start 4 downloads 'at the same time' but the files you're working with are pretty small, so just doing it sequentially would be very fast - less than a second to download all 4. – Jay Jan 27 '19 at 15:37

2 Answers2

0

Just access the root folder instead of the child directly, that way you'll obtain all the nodes/images in that folder something like this:

func firebaseSetUp(){

    let store = Storage.storage()
    let storeRef = store.reference()
    let userProfilesRef = storeRef.child("VDBackgroundFrames/")
    userProfilesRef.observe(.childAdded, with: { [weak self] (snapshot) -> Void in
        guard let strongSelf = self else { return }
        //Logic to extract urls...

    } 
}
Samuel Chavez
  • 768
  • 9
  • 12
  • Which version of swift are you using Sir...I am actually using 4.2 and there this code shows various issues. – Prez Jan 24 '19 at 18:39
  • I'm using xcode 9.4.1 Swift 4.1, which issues are you seeing? – Samuel Chavez Jan 24 '19 at 18:50
  • I am using xcode 10.1 and swift 4.2 .It is asking for some "Missing argument for parameter 'changeHandler' in call".When I add it ,it shows problem with "Value".Use of undeclared type 'Value'. Please,see updated code . – Prez Jan 24 '19 at 18:53
  • Nevermind, I confused this with RealTimeDatabase, and I found this post https://stackoverflow.com/questions/37335102/how-to-get-a-list-of-all-files-in-cloud-storage-in-a-firebase-app it's not impossible but you have to change the implementation quite a bit – Samuel Chavez Jan 24 '19 at 19:26
0

DownloadURL takes single string at a time. In case you want to show all the files inside a folder to a tableview like me, here is the full code:

   import UIKit import Firebase
My very First View Controller-

   class FolderList: UIViewController {
       var folderList: [StorageReference]?
        lazy var storage = Storage.storage()

       @IBOutlet weak var tableView : UITableView!

       override func viewDidLoad() {
           super.viewDidLoad()
   self.storage.reference().child("TestFolder").listAll(completion: {
   (result,error) in
               print("result is \(result)")
               self.folderList = result.items
               DispatchQueue.main.async {
                   self.tableView.reloadData()
               }
           })
       } }

   extension FolderList : UITableViewDataSource {
       func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return folderList?.count ?? 0
       }

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           guard let cell = tableView.dequeueReusableCell(withIdentifier: "FolderListCell", for:
   indexPath) as? FolderListCell else {return UITableViewCell()}
           cell.itemName.text = folderList?[indexPath.row].name
           return cell
       }

       func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           return 64.0
       } }


   extension FolderList : UITableViewDelegate {
       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
           guard let downloadVC = storyBoard.instantiateViewController(withIdentifier:
   "DownloadedItemView") as? DownloadedItemView else {
               return
           }
           downloadVC.storageRef = folderList?[indexPath.row]
           self.navigationController?.pushViewController(downloadVC, animated: true)
       } 
}





And here is you DownloadedItemView, which will open the images you selected from the list in a view:

import UIKit
import WebKit
import Firebase

class DownloadedItemView: UIViewController {
    @IBOutlet weak var webView : WKWebView!
    var downloadItemURL : String?
    var storageRef : StorageReference?


    override func viewDidLoad() {
        super.viewDidLoad()

        storageRef?.downloadURL(completion: {(downloadURL,error) in
            print("url is \(downloadURL)")
            DispatchQueue.main.async {
                guard let url = downloadURL else {return}
                let urlrequest = URLRequest(url: url)
                self.webView.load(urlrequest)
            }
        })
    }
}




Your each cell:

   class FolderListCell: UITableViewCell {

       @IBOutlet weak var itemName : UILabel!

   }
Rajlakshmi
  • 61
  • 1
  • 1
  • 6