update firebase database from current user - swift

I want to store image in firebase storage and create url than update value from my current database in firebase, but it didn't store in firebase database from current user. this is my code, where I do wrong?
fileprivate func saveToFirebase(image: UIImage) {
guard let imageData = image.jpegData(compressionQuality: 0.3) else { return }
let uuidName = UUID().uuidString
Storage.storage().reference().child("profile_images").child(uuidName).putData(imageData, metadata: nil) { (metadata, error) in
if let err = error {
print("💥 -- Failed to upload images -- 💥")
print("💥 -- \(err) -- 💥")
return
}
metadata?.storageReference?.downloadURL(completion: { (url, error) in
if let err = error {
print("💥 -- Failed to create URL -- 💥")
print("💥 -- \(err) -- 💥")
return
}
guard let profileImageURL = url?.absoluteString else { return }
guard let currentUID = Auth.auth().currentUser?.uid else { return }
let userProfileURL = ["profileImageURL": profileImageURL]
Database.database().reference().child("users").child(currentUID).updateChildValues(userProfileURL, withCompletionBlock: { (error, reference) in
if let err = error {
print("💥 -- Failed to create URL -- 💥")
print("💥 -- \(err) -- 💥")
return
}
print("✅ -- Successfully update user data -- ✅")
})
})
}
}

Firstly we can create an enum to represent the different failures that could occur. Some scenarios we consider to be errors (such as a guard else returning void, or a broken chained optional) fail silently. Here we can encapsulate the different failure scenarios that need to be handled.
enum ProfileImageUploadError: Error {
case unauthenticatedUser
case invalidImageData
case failedDataUpload(Error?)
case failedDownloadURL(Error?)
case failedProfileUpdate(Error?)
var localizedDescription: String {
switch self {
case .failedDataUpload: return "Failed to upload data"
case .failedDownloadURL: return "Failed to download URL"
case .failedProfileUpdate: return "Failed to update profile"
default: return "\(self)"
}
}
var underlyingError: Error? {
switch self {
case .failedDataUpload(let err): return err
case .failedDownloadURL(let err): return err
case .failedProfileUpdate(let err): return err
default: return nil
}
}
}
Next, we can immediately determine that we have an authenticated user and that the image data checks out. When a guard fails, we call the completion block passing the error case for that scenario. Keeping this up, we construct our references to the storage and database providers, and attempt the sequence catching errors.
Given there is no error initially uploading the data, we can assume the image has been uploaded. Also rather than using the optional metadata, we can use the storage ref we constructed earlier to download the URL.
As we continue through the sequence of operations, we're trying to sufficiently handle what we consider to be the errors, until successful completion upon which point we can return the URL saved to Firebase Database.
func uploadProfileImage(_ image: UIImage, completion: #escaping (URL?, ProfileImageUploadError?) -> ()) {
guard let currentUser = Auth.auth().currentUser else {
return completion(nil, .unauthenticatedUser)
}
guard let imageData = image.jpegData(compressionQuality: 0.3) else {
return completion(nil, .invalidImageData)
}
let storagePath = "profile_images/\(UUID().uuidString)"
let databasePath = "users/\(currentUser.uid)/profileImageURL"
let profileImageDataRef = Storage.storage().reference(withPath: storagePath)
let profileImageURLRef = Database.database().reference(withPath: databasePath)
profileImageDataRef.putData(imageData, metadata: nil) { (metadata, error) in
guard error == nil else {
return completion(nil, .failedDataUpload(error))
}
profileImageDataRef.downloadURL { (url, error) in
guard let profileImageURL = url?.absoluteString else {
return completion(nil, .failedDownloadURL(error))
}
profileImageURLRef.setValue(profileImageURL, withCompletionBlock: { (error, ref) in
guard error == nil else {
return completion(nil, .failedProfileUpdate(error))
}
completion(url, nil)
})
}
}
}
Lastly, this is how you would use the function inside your existing one.
fileprivate func saveToFirebase(image: UIImage) {
uploadProfileImage(image) { (url, error) in
if let error = error {
print("💥 -- \(error.localizedDescription) -- 💥")
print("💥 -- \(error.underlyingError.debugDescription) -- 💥")
} else {
print("✅ -- Successfully update user data -- ✅")
print("✅ -- \(url.debugDescription) -- ✅")
}
}
}
This is not tested
So to recap, some of the lines in your function can 'fail' silently, this can be resolved by sufficiently handling the 'optionals' and errors with completions or simply print statements. I think it is the chain of optional properties leading up to the URL download causing the issue – specifically the metadata property in metadata?.storageReference?.downloadURL.

Try with this..
useruid is firbase uid
let storageRef = Storage.storage().reference().child("profile_images").child(useruid)
storageRef.putData(imagedata, metadata: nil, completion: { (metadatas, error) in
if error != nil {
print("Couldn't Upload Image")
} else {
print("Uploaded")
print(metadatas!)
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print(error!)
return
}
if url != nil {
let imageurl = String(describing: url!)
print(imageurl) }
})
}
})

Related

Firebase getIDToken and how to use it in an API call

I have an API call that grabs json, but requires token authentication. Token auth works great, but when I try and pass the token along to the API function, it's coming back nil. I believe it's because Auth.auth().currentUser!.getIDToken(...) hasn't actually completed yet. Relevant code below... How do I modify this to
class SessionData : ObservableObject {
...
func token() -> String? {
var result: String? = nil
Auth.auth().currentUser!.getIDToken(completion: { (res, err) in
if err != nil {
print("*** TOKEN() ERROR: \(err!)")
} else {
print("*** TOKEN() SUCCESS: \(err!)")
result = res!
}
})
return result
}
...
}
class FetchPosts: ObservableObject {
#Published var posts = [Post]()
func load(api: Bool, session: SessionData) {
if api {
let url = URL(string: MyAPI.getAddress(token: session.token()!))!
URLSession.shared.dataTask(with: url) {(data, response, error) in
do {
if let postsData = data {
// 3.
let decodedData = try JSONDecoder().decode(Response.self, from: postsData)
DispatchQueue.main.async {
self.posts = decodedData.result
if decodedData.error != nil {
print("ERROR: \(decodedData.error!)")
session.json_error(error: decodedData.error!)
}
}
} else {
print("No data. Connection error.")
DispatchQueue.main.async {
session.json_error(error: "Could not connect to server, please try again!")
}
}
} catch {
print("* Error: \(error)")
}
}.resume()
} else {
let url = Bundle.main.url(forResource: "test", withExtension: "json")!
let data = try! Data(contentsOf: url)
let decoder = JSONDecoder()
if let products = try? decoder.decode([Post].self, from: data) {
self.posts = products
}
}
}
}
And this is how the .load function is called:
UserViewer(fetch: posts)
.transition(AnyTransition.slide)
.animation(.default)
.onAppear {
withAnimation{
posts.load(api: true, session: session)
}
}
.environmentObject(session)
Because getIDToken executes and returns asynchronously, you can't return directly from it. Instead, you'll need to use a callback function.
Here's a modification of your function:
func token(_ completion: #escaping (String?) -> ()) {
guard let user = Auth.auth().currentUser else {
//handle error
return
}
user.getIDToken(completion: { (res, err) in
if err != nil {
print("*** TOKEN() ERROR: \(err!)")
//handle error
} else {
print("*** TOKEN() SUCCESS: \(err!)")
completion(res)
}
})
}
Then, you can use it later on:
.onAppear {
session.token { token in
guard let token = token else {
//handle nil
return
}
withAnimation{
posts.load(api: true, session: session, token: token)
}
}
}
Modify your load to take a token parameter:
func load(api: Bool, session: SessionData, token: String) {
if api {
guard let url = URL(string: MyAPI.getAddress(token: token)) else {
//handle bad URL
return
}
Also, as you can see I'm doing in my code samples, I would try to get out of the habit of using ! to force unwrap optionals. If the optional is nil and you use !, your program will crash. Instead, familiarize yourself with guard let and if let and learn to handle optionals in a way that won't lead to a crash -- it's one of the great benefits of Swift.

Swift: upload multiple images to Firestore and assign URLs to object

I'm making an app whereby users post 2 images. I'm using Firebase for storage and as my database.
In my method to upload the images what I had wanted to do was to essentially use this method to return the URLs separately as well. I had written the following:
private func uploadImage(image: UIImage) -> URL? {
let randomName = UUID()
let storageRef = storage.reference().child("\(randomName)/png")
guard let uploadData = image.pngData() else { return nil}
var imageUrl: URL?
storageRef.putData(uploadData, metadata: nil) { (metadata, error) in
if error != nil {
print(error?.localizedDescription)
return
}
storageRef.downloadURL { (url, error) in
if error != nil {
print(error?.localizedDescription)
} else {
imageUrl = url
}
}
}
return imageUrl
}
And then I wrote the following 'post' method which is run when the submit button is tapped:
#objc func post() {
if let question = questionText.text,
let hashtagText = hashtagTextField.text,
let userHandle = Auth.auth().currentUser?.email,
let firstImage = left.image,
let secondImage = right.image,
let firstImageURL = uploadImage(image: firstImage)?.absoluteString,
let secondImageURL = uploadImage(image: secondImage)?.absoluteString
{
db.collection("posts").addDocument(data: [
"firstImage" : firstImageURL,
"secondImage" : secondImageURL,
"question" : question,
"hashtagText" : hashtagText,
"userHandle" : userHandle
]) { (error) in
if let e = error {
print("There was an issue saving data to Firestore, \(e)")
} else {
print("Successfully saved data")
self.dismiss(animated: true, completion: nil)
}
}
}
}
However, obviously the first method is not going to work as the closure is run after imageUrl is returned, therefore returning nil.
I've been trying to figure out how to manage this scenario - I had considered using a loop to populate an array of images but this got messy and I'm sure it is not the standard way to handle this. Any help would be greatly appreciated.
The return imageUrl is in the wrong place. It will return before Firebase has had time to store the image and return the url.
Additionally, the name of the file is not going to work. You currently have
storage.reference().child("\(randomName)/png") // xxxxx/png?
when it should be
storage.reference().child("\(randomName).png") // xxxxx.png
You can't 'return' data from a Firebase closure because firebase is asynchronous - a completion handler may possibly be a solution, but we don't know what the total use case is.
Let's assume you want want to store a users vacation picture in storage and then store that url in Firestore
private func uploadImage(image: UIImage) {
guard let uid = Auth.auth().currentUser?.uid else { return } //this users uid
let storageRef = storage.reference().child(uid).child("vacation.png")
//the path will be storage/users uid/vacation.png
guard let uploadData = image.pngData() else { return nil}
storageRef.putData(uploadData, metadata: nil) { (metadata, error) in
if error != nil {
print(error?.localizedDescription)
return
}
storageRef.downloadURL { (url, error) in
if error != nil {
print(error?.localizedDescription)
} else {
if url != nil {
//it's here where we store the imageUrl in Firestore
let dict = ["theUrl": url?.absoluteURL)]
let userRef = self.db.collection("users").document(uid)
//self.db points to *my* Firestore
userRef.collection("my_pics").addDocument(data: dict)
//will store in firstore/users/uid/docId/theUrl: the url
}
}
}
}
}

Swift Function Not Continuing after completion of Json Decoder within - "else if" issue?

I am using this function to call json information from a database - it gets the information fine.
But it does not continue after the "getHistoricalMonthlyData". so it will not get to the print("****** line 55"). (yes, I plan on making this a func once I figure out the issue.)
it will print the "print(i.stock)" fine.
I can share the "getHistoricalMonthlyData" code but it works fine and I doubt that is the issue.
I am not great with the completion handlers and I suspect that is the issue?
below is the "getHistoricalMonthlyData" function that I can not get past.
func calculateMonthPerformance (setting: settings) {
let set = setting
let u = User.getUser()
var i = Indexes()
getHistoricalMonthlyData(symbol: symbol, beg: set.monthBeg, end: set.monthEnd) { (json, error ) in
if let error = error {
print ("error", error)
} else if let json = json {
print ("success")
i.stock = json
print(47)
}
// this is fine
print(50)
print(i.stock)
}
// nothing at this point
print("****** line 55")
}
This is how the json function is set up and works great in another project.
it has a resume.
func getHistoricalMonthlyData(symbol: String, beg: Date, end: Date, completionHandler: #escaping ([HistoricalData]?, Error?) -> Void) {
let beg = beg.dateAtStartOf(.month).toFormat("yyyy-MM-dd")
let end = end.dateAtEndOf(.month).toFormat("yyyy-MM-dd")
let jsonUrl = "https://eodhistoricaldata.com/api/eod/\(symbol).US?from=\(beg)&to=\(end)&api_token=\(EOD_KEY)&period=eom&fmt=json"
guard let url = URL(string: jsonUrl) else {
print("Error: cannot create URL")
let error = BackendError.urlError(reason: "Could not create URL")
completionHandler(nil, error)
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil else {
completionHandler(nil, error!)
return
}
guard let jasonData = data else {
print("Error: did not receive data")
let error = BackendError.objectSerialization(reason: "No data in response")
completionHandler(nil, error)
return
}
do {
let historical = try JSONDecoder().decode([HistoricalData].self, from: jasonData )
completionHandler(historical, nil)
} catch let jsonErr {
print ("Error serializing json", jsonErr )
let error = BackendError.objectSerialization(reason: "Couldn't create a todo object from the JSON")
completionHandler(nil, error)
}
}.resume()
}
thanks.
if someone knows a better answer would love to hear about it.
I added a DispatchSemaphore to the code and it seems to work.
cheers.
let semaphore = DispatchSemaphore(value: 0)
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil else {
completionHandler(nil, error!)
return
}
guard let jasonData = data else {
let error = BackendError.objectSerialization(reason: "No data in response")
completionHandler(nil, error)
return
}
do {
let historical = try JSONDecoder().decode([HistoricalData].self, from: jasonData )
completionHandler(historical, nil)
} catch let jsonErr {
let error = BackendError.objectSerialization(reason: "Couldn't create a todo object from the JSON")
completionHandler(nil, error)
}
semaphore.signal()
}.resume()
_ = semaphore.wait(timeout: .distantFuture)

How to download URL from firebase? not receiving an error but not downloading either?

When using an image picker to grab the image selected and place in firebase storage I want to be able to download the URL and take place of the profile image inside the app. Unfortunately, when the process reaches the URLSession in the script nothing happens. There is not an error display nor does it dispatchQueue. The app will not crash but just skip over everything. Any ideas or suggestions to a code fix?
if let profileImageUploadedData = self.profileImage.image, let uploadData = profileImage.image?.jpegData(compressionQuality: 0.1)
{
storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil
{
print("Downdloading putData Error: \(error!.localizedDescription)")
return
}
storageRef.downloadURL(completion: { (url, error) in
if error != nil
{
print("DownloadURL ERROR \(error!.localizedDescription)")
return
}
if let profileImageUrl = url?.absoluteString
{
print("Profile image uploading...")
let values = ["profileImageUrl": profileImageUrl]
let url = URL(fileURLWithPath: profileImageUrl)
URLSession.shared.dataTask(with: url) { (data, response, error) in // ERROR OCCURING NEED TO FIX
if error != nil
{
print("* URL SESSIONS ERROR: \(error!)")
return
}
DispatchQueue.main.async
{
print("Trying to register profile")
self.registerUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
self.profileImage.image = UIImage(data: data!)
print("Dispatch: \(data!)")
}
print("Profile image successfull uploaded to storage")
}
}
})
}).resume()
print("** Profile Image Data Uploaded:\(profileImageUploadedData)")
}
}
func registerUserIntoDatabaseWithUID(uid: String, values: [String: AnyObject])
{
print("Registering to database")
let dataReference = Database.database().reference(fromURL: "URL String")
let usersReference = dataReference.child("users").child(uid)
usersReference.updateChildValues(values, withCompletionBlock: { (err, reference) in
if err != nil
{
print(err!)
return
}
// self.profileImage.image = values["profileImageUrl"] as? UIImage
// self.fetchProfileImage()
self.dismiss(animated: true, completion: nil)
print("Saved user sussccessfully in database")
})
}
}
This is a big question that is actually asking for a number of different answers so let's just focus on one;
How to authenticate a user, get a url stored in Firebase Database that
references an image stored in Firebase Storage, then download that
image.
Here we go
First - authenticate a user
Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
if let x = error {
//handle an auth error
} else {
if let user = auth?.user {
let uid = user.uid
self.loadUrlFromFirebaseDatabase(withUid: uid)
}
}
})
now the user is authenticated, get the image location url from Firebase Database
func loadUrlFromFirebaseDatabase(withUid: String) {
let thisUserRef = self.ref.child("users").child(withUid)
let urlRef = thisUserRef.child("url")
urlRef.observeSingleEvent(of: .value, with: { snapshot in
if let url = snapshot.value as? String {
self.loadImageUsingUrl(url: url)
} else {
print("no image for user")
}
})
}
Now that we have the location of the image in Firebase Storage, get it
func loadImageUsingUrl(url: String) {
let storage = Storage.storage()
let imageRef = storage.reference(forURL: url)
imageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("error downloading \(error)")
} else {
if let image = UIImage(data: data!) {
//do something with the image
} else {
print("no image")
}
}
}
}

How to get imageUrl from firebase storage and store in firebase database in Xcode 10 swift 4.2

Here is the code that I'm using
#objc func handleSignUp() {
guard let email = emailTextField.text, email.characters.count > 0 else { return }
guard let username = usernameTextField.text, username.characters.count > 0 else { return }
guard let password = passwordTextField.text, password.characters.count > 0 else { return }
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if let err = error {
print("Failed to create user:", err)
return
}
print("Successfully created user:", user?.user.uid)
// guard let image = self.plusPhotoButton.imageView?.image else { return }
// guard let uploadData = UIImageJPEGRepresentation(image, 0.3) else { return }
guard let uploadData = self.plusPhotoButton.imageView?.image?.jpegData(compressionQuality: 0.3) else{return}
let filename = NSUUID().uuidString
Storage.storage().reference().child("profile_images").child(filename).putData(uploadData, metadata: nil, completion: { (metadata, err) in
if let err = err {
print("Failed to upload profile image:", err)
return
}
//this code isn't correct for swift4.2
//guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return }
func getDownloadURL(from path: String, completion: #escaping (URL?, Error?) -> Void) {
let storageReference = Storage.storage().reference(forURL: "gs://instagramfirebase-60be5.appspot.com")
let storageRef = storageReference.child(path).downloadURL(completion: completion)
}
print("Successfully uploaded profile image")//,profileImageUrl
guard let uid = user?.user.uid else { return }
let dictionaryValues = ["username": username] as [String : Any]//, "profileImageUrl": profileImageUrl
let values = [uid: dictionaryValues]
Database.database().reference().child("users").updateChildValues(values, withCompletionBlock: { (err, ref) in
if let err = err {
print("Failed to save user info into db:", err)
return
}
print("Successfully saved user info to db")
})
})
})
}
Snapshots are as followed :
I am confused about how to get the imageUrl from firebase storage and store it in firebase database in Xcode 10 swift 4.2
Please help, Thank you for your time.
You can get the download url easily by using the following code inside the closure of your image-upload:
storageRef.downloadURL(completion: { (url, error) in
if let err = error{
// error happened - implement code to handle it
print(err)
} else {
// no error happened; so just continue with your code
print(url?.absoluteString) // this is the actual download url - the absolute string
}
Please note: Don't store the downloadURL on its own as Firebase can change tokens of the downloadURL, I'd suggest to always grab a hold of the full storage path.
I hope I was able to help.