Just updated my cocoa pods thus updating firebase. This line of code is the old way, which is wrong now:
let downloadURL = metaData!.downloadURL()!.absoluteString
let values: Dictionary<String, Any> = ["download_url": downloadURL]
The following code is the correct way now to extract the URL string. Yet, I need help on how I can put that string into my array to save to firebase.
storageRef.downloadURL(completion: {(url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
let downloadURL = url?.absoluteString
})
let values: Dictionary<String, Any> = ["download_url": downloadURL]
How I save "values" as a child
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((self.loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
let storageRef = Storage.storage().reference().child("posts_requests").child((self.loggedInUser?.uid)!).child(snapshot.childSnapshot(forPath: "uid").value as! String).child(snapshot.childSnapshot(forPath: "uid").value as! String).child(snapshot.childSnapshot(forPath: "imageID").value as! String)
storageRef.delete(completion: { error in
if let error = error {
print(error)
} else {
print("Successful Delete")
}
})
}
}
Using the answers below...
When I use the submitted answers below I get a print out saying "User does not have permission to access gs://shoppeer-e7270.appspot.com/(null)." All I am trying to accomplish is grabbing that URL string and adding it to my "values" which is a Dictionary.
My full code for image upload as well as saving as a child
let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
let usersRef = Database.database().reference().child("Businesses")
let databaseRef = Database.database().reference()
let imageName = NSUUID().uuidString
let photoRef = photosRef.child("\(uid)")
let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
var downloadURLSting = String()
photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
if let error = error {
print("there was an error")
print(error.localizedDescription)
return
} else {
// store downloadURL
storage.reference().downloadURL(completion: {(url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
let downloadURL = url?.absoluteString
let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
// store downloadURL at database
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
// reset caption field
self.descriptionTextView.text = ""
// reset placeholder image
self.imageView.image = UIImage(named: "filterPlaceholder")
MBProgressHUD.hide(for: self.view, animated: true)
let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
self.present(viewConrolller, animated: true, completion: nil)
}
}
})
}
}
This works just downloadURL string is nil
let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
let usersRef = Database.database().reference().child("Businesses")
let databaseRef = Database.database().reference()
let imageName = NSUUID().uuidString
let photoRef = photosRef.child("\(uid)")
let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
if let error = error {
print("there was an error")
print(error.localizedDescription)
return
} else {
// store downloadURL
photoRef.downloadURL(completion: {(url, error) in
if error != nil {
guard let downloadURL = url?.absoluteString else { return }
let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
// store downloadURL at database
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
// reset caption field
self.descriptionTextView.text = ""
// reset placeholder image
self.imageView.image = UIImage(named: "filterPlaceholder")
MBProgressHUD.hide(for: self.view, animated: true)
let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
self.present(viewConrolller, animated: true, completion: nil)
}
}
} else {
print(error!.localizedDescription)
print("error")
return
}
})
}
}
values is a Dictionary, not an Array, but if you want to add downloadURL to it, you'll need to do that inside the completion handler of storageRef.downloadURL(completion:), since that's an asynchronous method.
storageRef.downloadURL(completion: {(url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
let downloadURL = url?.absoluteString
let values: Dictionary<String, Any> = ["download_url": downloadURL]
})
The download URL is only available inside the completion handler.
storageRef.downloadURL(completion: {(url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
let downloadURL = url?.absoluteString
let values: Dictionary<String, Any> = ["download_url": downloadURL]
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((self.loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
...
Related
So I'm trying to store data retrieved from my Firestore database into an object. My database has a collection of users, and each user has a collection of classes. I want to be able to get the logged in users collection of classes and store them in an array of objects. Most of what I've tried so far can pull data but it won't save it into anything because its able access the data from within the completion handler. Any help would be great, here's the code I'm working with rn:
db.collection("users").whereField("uid", isEqualTo: uid).addSnapshotListener { (querySnapshot, error) in
if error == nil && querySnapshot != nil {
let docId = querySnapshot?.documents[0].documentID
db.collection("users").document(docId!).collection("classes").addSnapshotListener { (querySnap, error) in
guard let documents = querySnap?.documents else{print("No Classes");return}
var imageData:UIImage?
retrievedClasses = documents.map { (querySnap) -> UserClass in
let data = querySnap.data()
if let decodedData = Data(base64Encoded: data["class_img"] as! String, options: .ignoreUnknownCharacters){
imageData = UIImage(data: decodedData)
}
return UserClass.init(name: data["class_name"] as! String, desc: data["class_desc"] as! String, img: imageData!, color: data["class_color"] as! String, link: data["class_link"] as! String, location: data["class_location"] as! GeoPoint, meetingTime: data["meeting_time"] as! Dictionary<String,String>)
}
print(retrievedClasses[0].printClass())
}
}
}
As I understand you need to do something like this:
func getUserClasses(for userID: String, completion: #escaping (Result<[UserClass], Error>) -> Void) {
db.collection("users").whereField("uid", isEqualTo: userID).addSnapshotListener { (querySnapshot, error) in
if error == nil && querySnapshot != nil {
let docId = querySnapshot?.documents[0].documentID
db.collection("users").document(docId!).collection("classes").addSnapshotListener { (querySnap, error) in
guard let documents = querySnap?.documents else {
print("No Classes")
completion(.failure("No Classes"))
return
}
let retrievedClasses = documents.map { (querySnap) -> UserClass in
let data = querySnap.data()
var imageData: UIImage?
if let decodedData = Data(base64Encoded: data["class_img"] as! String,
options: .ignoreUnknownCharacters) {
imageData = UIImage(data: decodedData)
}
let user = UserClass(name: data["class_name"] as! String,
desc: data["class_desc"] as! String,
img: imageData!,
color: data["class_color"] as! String,
link: data["class_link"] as! String,
location: data["class_location"] as! GeoPoint,
meetingTime: data["meeting_time"] as! Dictionary<String,String>)
return user
}
completion(.success(retrievedClasses))
}
} else {
completion(.failure("Request Error"))
}
}
}
Then you will able to use data after request completion:
getUserClasses(for: "123") { result in
switch result {
case .success(let allClasses):
retrievedClasses = allClasses // retrievedClasses is a property in your class which you are going to use
if !retrievedClasses.isEmpty() {
print(retrievedClasses[0].printClass())
}
case .failure(let error):
print(error)
}
}
In my page named service, xcode points to the user and gives an error. but it doesn't work. What do you think should I change?
my user is already optional. I think it is an index problem but I don't know how to solve it I would appreciate it if you could help.where do you think the problem
message.swift
import Firebase
struct Message {
let text: String
let toId: String
let fromId: String
var timestamp: Timestamp!
var user: User?
let isFromCurrentUser :Bool
init(dictionary: [String: Any]) {
self.text = dictionary["text"] as? String ?? ""
self.toId = dictionary["toId"] as? String ?? ""
self.fromId = dictionary["fromId"] as? String ?? ""
self.timestamp = dictionary["timestamp"] as? Timestamp ?? Timestamp(date: Date())
self.isFromCurrentUser = fromId == Auth.auth().currentUser?.uid
}
}
struct Conversation {
let user: User
let message : Message
}
Service.Swift
import Firebase
struct Service {
static func fetchUsers (completion: #escaping([User]) -> Void) {
var users = [User] ()
COLLECTION_USERS.getDocuments { (snapshot, error) in
snapshot?.documents.forEach({ (document) in
let dictionary = document.data()
let user = User(dictionary: dictionary)
users.append(user)
completion(users)
})
}
}
static func fetchUser(widhtUid uid: String, completion:#escaping([User]) ->Void) {
COLLECTION_USERS.document(uid).getDocument { (snapshot, error) in
guard let dictionary = snapshot?.data() else {return}
let user = User(dictionary: dictionary)
completion(user)
}
}
static func fetchConversations (completion: #escaping([Conversation]) ->Void) {
var conversations = [Conversation]()
guard let uid = Auth.auth().currentUser?.uid else {return}
let query = COLLECTION_MESSAGES.document(uid).collection("recent-messages").order(by: "timestamp")
query.addSnapshotListener { (snapshot, error) in
snapshot?.documentChanges.forEach({ change in
let dictionary = change.document.data()
let message = Message(dictionary: dictionary)
self.fetchUser(widhtUid: message.toId) { user in
let conversation = Conversation(user:user, message: message)
conversations.append(conversation)
completion(conversations)
}
})
}
}
static func fetchMessages (forUser user: User, completion: #escaping([Message])-> Void) {
var messages = [Message]()
guard let currentUid = Auth.auth().currentUser?.uid else {return}
let query = COLLECTION_MESSAGES.document(currentUid).collection(user.uid).order(by: "timestamp")
query.addSnapshotListener{(snapshot,error) in
snapshot?.documentChanges.forEach({ change in
if change.type == .added {
let dictionary = change.document.data ()
messages.append(Message(dictionary: dictionary))
completion(messages)
}
})
}
}
static func uploadMessage(message: String, to user: User, completion: ((Error?)->Void)?) {
guard let currentUid = Auth.auth().currentUser?.uid else {return}
let data = ["text": message,
"fromId": currentUid,
"toId": user.uid,
"timestamp" : Timestamp(date: Date())] as [String : Any]
COLLECTION_MESSAGES.document(currentUid).collection(user.uid).addDocument(data:data) { _ in
COLLECTION_MESSAGES.document(user.uid).collection(currentUid).addDocument(data:data,completion:completion)
COLLECTION_MESSAGES.document(currentUid).collection("recent- messages").document(user.uid).setData(data)
COLLECTION_MESSAGES.document(user.uid).collection("recent- messages").document(currentUid).setData(data)
}
}
}
In this method:
static func fetchUser(widhtUid uid: String, completion:#escaping ([User]) -> Void)
The completion closure's parameter should be a User, not an array of users - [User].
Xcode should point you to the line where this error happens...
Anyway, here
static func fetchUser(widhtUid uid: String, completion:#escaping([User]) ->Void) {
COLLECTION_USERS.document(uid).getDocument { (snapshot, error) in
guard let dictionary = snapshot?.data() else {return}
let user = User(dictionary: dictionary)
completion(user)
}
}
Your completion:#escaping([User]) ->Void) expects an array [User] , but you invoke it with just one User object here completion(user)
How do I print out the image from
print((result! as AnyObject).value(forKey: "picture.data.url") as? Any)
I know that the following prints out the data and url but I just need the URL section.
print((result! as AnyObject).value(forKey: "picture") as? Any)
any advise?
I am using FacebookLogin SDK
NOTE: You can create the image URL on your own if you have the userId,
let facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"
Here is how you can get id,name,email and profile picture url from facebook login. I use this code in my app and it works.
func fetchFacebookFields() {
LoginManager().logIn(permissions: ["email","public_profile"], from: nil) {
(result, error) -> Void in
if let error = error {
print(error.localizedDescription)
return
}
guard let result = result else { return }
if result.isCancelled { return }
else {
GraphRequest(graphPath: "me", parameters: ["fields" : "first_name, last_name, email"]).start() {
(connection, result, error) in
if let error = error {
print(error.localizedDescription)
return
}
if
let fields = result as? [String:Any],
let userID = fields["id"] as? String,
let firstName = fields["first_name"] as? String,
let lastName = fields["last_name"] as? String,
let email = fields["email"] as? String
{
let facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"
print("firstName -> \(firstName)")
print("lastName -> \(lastName)")
print("email -> \(email)")
print("facebookProfileUrl -> \(facebookProfileUrl)")
APPDELEGATEOBJ.makeRootVC(vcName : "MainTabBarVC")
}
}
}
}
}
I have updated my code from "metaData?.downloadURL" to my current code underneath. Problem is that the image saves to the Storage, but does not save to the Database and I get a print out saying "Object posts/wqyMJTs438XsJWq13ciAYyzs9Am2/wqyMJTs438XsJWq13ciAYyzs9Am2 does not exist."
let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
let usersRef = Database.database().reference().child("Businesses")
let databaseRef = Database.database().reference()
let imageName = NSUUID().uuidString
let photoRef = photosRef.child("\(uid)")
let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
if let error = error {
print("there was an error")
print(error.localizedDescription)
return
} else {
// store downloadURL
photoRef.downloadURL(completion: {(url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
let downloadURL = url?.absoluteString
let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
// store downloadURL at database
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
// reset caption field
self.descriptionTextView.text = ""
// reset placeholder image
self.imageView.image = UIImage(named: "filterPlaceholder")
MBProgressHUD.hide(for: self.view, animated: true)
let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
self.present(viewConrolller, animated: true, completion: nil)
}
}
})
}
}
This code works, yet downloadURL returns nil
let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
let usersRef = Database.database().reference().child("Businesses")
let databaseRef = Database.database().reference()
let imageName = NSUUID().uuidString
let photoRef = photosRef.child("\(uid)")
let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
if let error = error {
print("there was an error")
print(error.localizedDescription)
return
} else {
// store downloadURL
photoRef.downloadURL(completion: {(url, error) in
if error != nil {
guard let downloadURL = url?.absoluteString else { return }
let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
// store downloadURL at database
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
// reset caption field
self.descriptionTextView.text = ""
// reset placeholder image
self.imageView.image = UIImage(named: "filterPlaceholder")
MBProgressHUD.hide(for: self.view, animated: true)
let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
self.present(viewConrolller, animated: true, completion: nil)
}
}
} else {
print(error!.localizedDescription)
print("error")
return
}
})
}
}
I followed the Firebase tutorial by Ray Wenderlich (Link) and adopted his way of initializing the object (in my case of type "Location") with the snapshot from the observe-method:
class Location:
init(snapshot: FIRDataSnapshot) {
identifier = snapshot.key
let snapshotValue = snapshot.value as! [String : AnyObject]
type = snapshotValue["type"] as! String
name = snapshotValue["name"] as! String
address = snapshotValue["address"] as! String
latitude = Double(snapshotValue["latitude"] as! String)!
longitude = Double(snapshotValue["longitude"] as! String)!
avatarPath = snapshotValue["avatarPath"] as! String
ref = snapshot.ref
}
LocationsViewController:
databaseHandle = locationsRef?.queryOrdered(byChild: "name").observe(.value, with: { (snapshot) in
var newLocations:[Location] = []
for loc in snapshot.children {
let location = Location(snapshot: loc as! FIRDataSnapshot)
newLocations.append(location)
}
self.locations = newLocations
self.tableView.reloadData()
})
This really works like a charm, but now I'm trying to load the image stored under the storage reference "avatarPath".
My attempt worked but the images take a ling time to load. Is there a better way/place to load these images?
My attempt 1:
databaseHandle = locationsRef?.queryOrdered(byChild: "name").observe(.value, with: { (snapshot) in
var newLocations:[Location] = []
for loc in snapshot.children {
let location = Location(snapshot: loc as! FIRDataSnapshot)
newLocations.append(location)
}
self.locations = newLocations
self.tableView.reloadData()
//Load images
for loc in self.locations {
let imagesStorageRef = FIRStorage.storage().reference().child(loc.avatarPath)
imagesStorageRef.data(withMaxSize: 1*1024*1024, completion: { (data, error) in
if let error = error {
print(error.localizedDescription)
} else {
loc.avatarImage = UIImage(data: data!)!
self.tableView.reloadData()
}
})
}
})
My 2nd Attempt (inside Location class):
init(snapshot: FIRDataSnapshot) {
identifier = snapshot.key
let snapshotValue = snapshot.value as! [String : AnyObject]
type = snapshotValue["type"] as! String
name = snapshotValue["name"] as! String
address = snapshotValue["address"] as! String
latitude = Double(snapshotValue["latitude"] as! String)!
longitude = Double(snapshotValue["longitude"] as! String)!
avatarPath = snapshotValue["avatarPath"] as! String
ref = snapshot.ref
super.init()
downloadImage()
}
func downloadImage() {
let imagesStorageRef = FIRStorage.storage().reference().child(self.avatarPath)
imagesStorageRef.data(withMaxSize: 1*1024*1024, completion: { (data, error) in
if let error = error {
print(error.localizedDescription)
} else {
self.avatarImage = UIImage(data: data!)!
}
})
}
Thank you in advance!
Nico
The best way you can accomplish that is to load asynchronous inside the loading of the cell function. I mean:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
DispatchQueue.main.async {
let imagesStorageRef = FIRStorage.storage().reference().child(self.locations[indexPath.row].avatarPath)
imagesStorageRef.data(withMaxSize: 1*1024*1024, completion: { (data, error) in
if let error = error {
print(error.localizedDescription)
} else {
locations[indexPath.row].avatarImage = UIImage(data: data!)!
tableView.reloadRows(at indexPaths: [indexPath], with animation: .none)
}
})
}
}
In first attempt try changing your code as:
DispatchQueue.main.async {
for loc in self.locations {
let imagesStorageRef = FIRStorage.storage().reference().child(loc.avatarPath)
imagesStorageRef.data(withMaxSize: 1*1024*1024, completion: { (data, error) in
if let error = error {
print(error.localizedDescription)
} else {
loc.avatarImage = UIImage(data: data!)!
self.tableView.reloadData()
}
})
}
}