0

I'm new to firebase and I wanted to try using a realtime database. It's been fairly easy to use until it's time to fetch the data from the database. I'm currently stuck at trying to get the data back into an array to provide my tableView with data.

enter image description here

Each image is saved with a timestamp. I've fetching it in different ways, it ends up printing nil or breaking the app. But when i use a breakpoint, I'm able to see the same data.

func downloadFromFirebase(completion: @escaping (Bool, Error?) -> Void) {
        ref.child("ImageDetails/").observeSingleEvent(of: .value) { (snapshot) in
            
            guard let value = snapshot.value as? [String:Any] else { return }
            let name = value["username"] as! String
        completion(true, nil)
        }
    }

1 Answer 1

1

I don't understand your question so I will write a code in order to retrieve the data from realtime database and to save it in an array.

You said "Each image is saved with a timestamp", so I assume you want to retrieve an array of data containing images(imageURL, etc) right?

we create a structure based on your database

struct dataStructure {
var Description:String?
var imageURL:String?
var Portfolio:String?
var createdAt:String?
var Instagram:String?
var profileImageUrl:String?
var Twitter:String?
var Username:String?
}
// now we create an array of this struct

var arrayOfData = [dataStructure]()

func downloadFromFirebase(completion:@escaping (Bool ,Error?)-> Void) {
      ref.child("ImageDetails/").observeSingleEvent(of: .value) { snapshot in
      guard let value = snapshot.value as? [[String:Any]] else { return }

// we add each element of value in the arrayOfData
        for element in value {
            guard let name = value["username"] as! String,
                  let Description = value["Description"] as! String, 
                  let imageURL = value["imageURL"] as! String, 
                  let Portfolio =  value["Portfolio"] as! String,
                  let creationDate = value["createdAt"] as! String, 
                  let Instagram = value["Instagram"] as! String, 
                  let profileImageUrl = value["profileImageUrl"] as! String, 
                  let Twitter = value["Twitter"] as! String , 
                  let Username = value["Username"] as! String else {
                   completion(false)
                   return
                     }

  arrayOfData.append(dataStructure(name: name, 
                                   Description: Description, 
                                   imageURL: imageURL,
                                   Portfolio: Portfolio ,
                                   creationDate:creationDate ,
                                   Instagram: Instagram, 
                                   profileImageUrl: profileImageUrl,
                                   Twitter: Twitter))

        }


        completion(true)
        }
}

at the end you will have an array with all your data

here is the code if you want to take the data from this function in your completion

func downloadFromFirebase(completion:@escaping (Result<dataStructure, Error>) {
      ref.child("ImageDetails/").observeSingleEvent(of: .value) { snapshot in
      guard let value = snapshot.value as? [[String:Any]] else { return }

// we add each element of value in the arrayOfData
        for element in value {
            guard let name = value["username"] as! String,
                  let Description = value["Description"] as! String, 
                  let imageURL = value["imageURL"] as! String, 
                  let Portfolio =  value["Portfolio"] as! String,
                  let creationDate = value["createdAt"] as! String, 
                  let Instagram = value["Instagram"] as! String, 
                  let profileImageUrl = value["profileImageUrl"] as! String, 
                  let Twitter = value["Twitter"] as! String , 
                  let Username = value["Username"] as! String else {
                   completion(.failure(error)
                   return
                     }

  arrayOfData.append(dataStructure(name: name, 
                                   Description: Description, 
                                   imageURL: imageURL,
                                   Portfolio: Portfolio ,
                                   creationDate:creationDate ,
                                   Instagram: Instagram, 
                                   profileImageUrl: profileImageUrl,
                                   Twitter: Twitter))

        }

       /* we provide the array with the new data in the completion at the 
          end of the loop */
        completion(.success(arrayOfData))
        }
}

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this worked. Sorry the question was confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.