I am working on an application about restaurant. But having some trouble with the Firebase database structure. The database structure of my code is like this:
restaurants {
AutoID {
name: "nnnnn",
phone:"88888888" }
AutoID {
name: "nnnnn",
phone:"88888888" }
image {
AutoID: URL
AutoID: URL }
}
How can I change the structure to become like this:
restaurants {
AutoID {
name: "nnnnn",
phone:"88888888",
image: URL }
AutoID {
name: "nnnnn",
phone:"88888888",
image: URL }
The current code are:
@IBAction func saveButton(_ sender: Any)
{
addRestaurant()
}
func addRestaurant()
{
ref = FIRDatabase.database().reference().child("restaurants")
let key = ref?.childByAutoId().key
let name = addName.text
let phone = addPhone.text
ref?.child(key!).setValue(["name": name, "phone": phone])
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
addImage.image = image
var data = Data()
data = UIImagePNGRepresentation(image!)!
let uniqueName = NSUUID().uuidString
let imageRef = FIRStorage.storage().reference().child("restaurantImage").child("\(uniqueName)")
imageRef.put(data, metadata: nil).observe(.success){(snapshot) in
let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
let imageDBRef = FIRDatabase.database().reference().child("restaurants/image")
let key = imageDBRef.childByAutoId().key
imageDBRef.child(key).setValue(downloadURL)
}
self.dismiss(animated: true, completion: nil)
}
Thank you very much !
As you told that you are first Storing image in FIRStorage
then adding name
and phone
what you need to do is inside the closure of imageRef
you need to store the reference of your stored image URL, so declare one instance property in your controller and set it inside that closure and also don't set image url in DB that time, after that with save button use that newly created property to post name, phone and url at same time.
var imageURL = ""
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
addImage.image = image
var data = Data()
data = UIImagePNGRepresentation(image!)!
let uniqueName = NSUUID().uuidString
let imageRef = FIRStorage.storage().reference().child("restaurantImage").child("\(uniqueName)")
imageRef.put(data, metadata: nil).observe(.success){(snapshot) in
self.imageURL = snapshot.metadata?.downloadURL()?.absoluteString
//Remove other code
}
self.dismiss(animated: true, completion: nil)
}
Now with your addRestaurant
add key for image
and set its value with imageURL
func addRestaurant()
{
ref = FIRDatabase.database().reference().child("restaurants")
let key = ref?.childByAutoId().key
let name = addName.text
let phone = addPhone.text
ref?.child(key!).setValue(["name": name, "phone": phone, "image": imageURL])
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.