How to retrieve image from firebase storage Swift 4 ios - ios

I am trying to retrieve an image from firebase storage but the image that I retrieve always is nil for some reason.
var ref: DatabaseReference!
var storageRef: StorageReference!
var hallData = [Hall]()
override func viewDidLoad() {
let refHandle = Database.database().reference().child("hallData").observe(DataEventType.value, with: { (snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
 let values = Array(postDict.values)
//print(values)
let valueDict = values as! [[String:Any]]
for i in valueDict
{
var name = i["name"] as! String
var address = i["address"] as! String
var capacity = i["capacity"] as! String
var decorations = i["decorations"] as! String
var highPrice = i["highPrice"] as! String
var lowPrice = i["lowPrice"] as! String
var catering = i["catering"] as! String
var email = i["email"] as! String
self.storageRef = Storage.storage().reference().child("images").child(email)
var image: UIImage!
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("PLASESEE")
print(error.localizedDescription)
// Uh-oh, an error occurred!
} else {
// Data for "images/island.jpg" is returned
let image = UIImage(data: data!)
}
}
print(image)
self.hallData.append(Hall(name2: name, capacity2: capacity, lowPrice2: lowPrice, highPrice2: highPrice, catering2: catering,decorations2: decorations, address2:address, image2: image, email2: email))
}
})
}
I dont understand what I am doing wrong, I followed the api on firebase storage, checked out a lot of tutorials but I keep getting nil

The issue is that you're attempting to work with the image var outside the getData closure.
self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("PLASESEE")
print(error.localizedDescription)
// Uh-oh, an error occurred!
} else {
// Data for "images/island.jpg" is returned
let image = UIImage(data: data!)
} <- closure ends here and image is only valid above this
}
print(image) <- image may not be populated at this point
self.hallData.append... image
}
That closure is asynchronous and the call to self.hallData.append... will occur way before the image var is populated within the closure. Code is much faster than the internet
Move that statement inside the closure, right after the let image = and it should work.
self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("An error occurred in downloading the image")
print(error.localizedDescription)
} else {
let image = UIImage(data: data!)
self.hallData.append... image
//reload your tableView or UI as self.hallData is now valid
}
}
If you do it this way, the preceeding var image: UIImage! can be removed as it doesn't have a function. Otherwise, remove the let before let image = within the closure.

Related

Save two different images at the same time on firebase

I try to save two different images at the same time at one storage location
This is my function to save the information
var text: String = ""
var addedByUser: String?
var userImage: UIImage?
var jobImage: UIImage!
var downloadURL: String?
var userDownloadURL: String?
let ref: DatabaseReference!
init(text: String? = nil, jobImage: UIImage? = nil, addedByUser: String? = nil, userImage: UIImage? = nil) {
self.text = text!
self.jobImage = jobImage
self.addedByUser = addedByUser
self.userImage = userImage
ref = Database.database().reference().child("jobs").childByAutoId()
}
init(snapshot: DataSnapshot){
ref = snapshot.ref
if let value = snapshot.value as? [String : Any] {
text = value["text"] as! String
addedByUser = value["addedByUser"] as? String
downloadURL = value["imageDownloadURL"] as? String
userDownloadURL = value["imageUserDownloadURL"] as? String
}
}
func save() {
let newPostKey = ref.key
// save jobImage
if let imageData = jobImage.jpegData(compressionQuality: 0.5) {
let storage = Storage.storage().reference().child("jobImages/\(newPostKey)")
storage.putData(imageData).observe(.success, handler: { (snapshot) in
self.downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
let postDictionary = [
"imageDownloadURL" : self.downloadURL!,
"imageUserDownloadURL" : self.userDownloadURL!,
"text" : self.text,
"addedByUser" : self.addedByUser!
] as [String : Any]
self.ref.setValue(postDictionary)
})
}
}
I tried following code
if let imageData = jobImage.jpegData(compressionQuality: 0.5), ((userImage?.jpegData(compressionQuality: 0.5)) != nil) {
But it's not working as then nothing get's saved in the database...
Do you have any ideas how I can solve it?
I believe the question is how do I upload an image to two different locations. It's unclear why there's an observe function so this answer ignores that as it may not be needed.
Starting with your code, your save function will look like this
func save() {
self.uploadImageTask(imageName: "my_image.png", toLocation: "jobImage")
self.uploadImageTask(imageName: "my_image.png", toLocation: "anotherLocation")
}
and then the upload function
func uploadImageTask(imageName: String, toLocation: String) {
let theImage = UIImage(named: imageName) //set up your image here
let data = UIImagePNGRepresentation(theImage)! //we're doing a PNG
let storage = Storage.storage()
let storageRef = storage.reference()
let locationRef = storageRef.child("images").child(toLocation)
let imageLocationRef = locationRef.child(imageName)
// Upload the file to the path "images/location/imageName"
let uploadTask = locationRef.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
print("error while uploading")
return
}
let size = metadata.size // Metadata contains file metadata such as size, content-type.
print(size)
locationRef.downloadURL { (url, error) in
guard let downloadURL = url else {
print("an error occured after uploading and then downloading")
return
}
let x = downloadURL.absoluteString
print(x) //or build a dict and save to Firebase
}
}
}
the result is an image stored at
/images/jobImage/my_image.png
/images/anotherLocation/my_image.png
and it will also print the path to each image, which could be stored in Firebase.

Swift / iOS Core Data - Save Large Amount of Records in Background Thread

I am working on an iOS app (Swift) which fetches a huge amount of data (21000 records) through a web service (in chunks of 1000 records per request). And at the end of each request I need to store those 1000 records in core data. This is what I have done so far:
AppDelegate
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "ABC")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
Global Variables (At the end of AppDelegate)
let global_appDelegate = UIApplication.shared.delegate as! AppDelegate
let global_context = global_appDelegate.persistentContainer.viewContext
ViewController
func downloadMedicines( offset: Int64 )
{
let total_drugs_count = UserDefaults.standard.integer(forKey: "drug_count")
var dCount: Int64 = offset
ClassNetworkService.request_data(TagText: "Medicines", myURL: "&resource=meds", myPostParam: "dcount=\(dCount)&token=\(UserDefaults.standard.getToken())", showAlert: nil) { ( data, thread_error_title, thread_error_message ) in
DispatchQueue.main.async {
print("now its ----> M E D I C I N E S -#- \(dCount)")
if ( thread_error_title == "" )
{
if let _d_count = data["dcount"] as? Int64
{
dCount = _d_count
}
if let _data = data["data"] as? NSArray
{
for tmp_data in _data
{
if let tmp_data_dictionary = tmp_data as? NSDictionary
{
let table_medicine = Medicine(context: global_context)
table_medicine.id = Int64(tmp_data_dictionary["mID"] as! String)!
table_medicine.supplier = (tmp_data_dictionary["supplier"] as! String)
table_medicine.displayNdc = (tmp_data_dictionary["display_ndc"] as! String)
table_medicine.medispanGpi = (tmp_data_dictionary["medispan_gpi"] as! String)
table_medicine.medicationName = (tmp_data_dictionary["selldescription"] as! String)
table_medicine.genericTherapClass = (tmp_data_dictionary["generic_therap_class"] as! String)
table_medicine.ahfsTherapClass = (tmp_data_dictionary["ahfs_therap_class"] as! String)
table_medicine.keyword = (tmp_data_dictionary["keyword"] as! String)
table_medicine.memberNumber = Int64(tmp_data_dictionary["member_number"] as! String)!
table_medicine.notes = (tmp_data_dictionary["notes"] as! String)
table_medicine.pricePerUnit = Double(tmp_data_dictionary["price_per_unit"] as! String)!
table_medicine.drugOrder = Int64(tmp_data_dictionary["drug_order"] as! String)!
table_medicine.displayedStrength = (tmp_data_dictionary["displayed_strength"] as! String)
table_medicine.displayUnits = (tmp_data_dictionary["display_units"] as! String)
table_medicine.expDate = (tmp_data_dictionary["exp_date"] as! String)
table_medicine.soldUnits = (tmp_data_dictionary["sold_units"] as! String)
table_medicine.soldUnitsPlural = (tmp_data_dictionary["sold_units_p"] as! String)
table_medicine.pkgQty = (tmp_data_dictionary["pkg_qty"] as! String)
table_medicine.genericInd = (tmp_data_dictionary["generic_ind"] as! String)
table_medicine.defaultQty = Int64(tmp_data_dictionary["default_qty"] as! String)!
global_appDelegate.saveContext()
}
}
}
// download and sync more medicines here
let request_medicine = NSFetchRequest<NSFetchRequestResult>(entityName: "Medicine")
do
{
let all_medicine = try global_context.fetch(request_medicine)
if ( all_medicine.count < total_drugs_count ) // total_drugs_count
{
self.downloadMedicines( offset: dCount )
}
else
{
// syncing complete
}
}
catch
{
print (error)
}
}
}
}
}
As long as my web service is being processed my UI stands smooth but as soon as data save logic executes my UI freezes. I want to get rid of this UI freeze problem. I know it can be done by using background threads or something like this but I am still unable to figure out any solution. Any help would be greatly appreciated. Thanks
Thank you all for your suggestions. I managed to solve this issue. Posting my code below in case someone else needs it.
// Creates a task with a new background context created on the fly
global_appDelegate.persistentContainer.performBackgroundTask { (context) in
for tmp_data in _data
{
if let tmp_data_dictionary = tmp_data as? NSDictionary
{
let table_medicine = Medicine(context: context)
table_medicine.id = Int64(tmp_data_dictionary["mID"] as! String)!
table_medicine.supplier = (tmp_data_dictionary["supplier"] as! String)
table_medicine.displayNdc = (tmp_data_dictionary["display_ndc"] as! String)
table_medicine.medispanGpi = (tmp_data_dictionary["medispan_gpi"] as! String)
table_medicine.medicationName = (tmp_data_dictionary["selldescription"] as! String)
table_medicine.genericTherapClass = (tmp_data_dictionary["generic_therap_class"] as! String)
table_medicine.ahfsTherapClass = (tmp_data_dictionary["ahfs_therap_class"] as! String)
table_medicine.keyword = (tmp_data_dictionary["keyword"] as! String)
table_medicine.memberNumber = Int64(tmp_data_dictionary["member_number"] as! String)!
table_medicine.notes = (tmp_data_dictionary["notes"] as! String)
table_medicine.pricePerUnit = Double(tmp_data_dictionary["price_per_unit"] as! String)!
table_medicine.drugOrder = Int64(tmp_data_dictionary["drug_order"] as! String)!
table_medicine.displayedStrength = (tmp_data_dictionary["displayed_strength"] as! String)
table_medicine.displayUnits = (tmp_data_dictionary["display_units"] as! String)
table_medicine.expDate = (tmp_data_dictionary["exp_date"] as! String)
table_medicine.soldUnits = (tmp_data_dictionary["sold_units"] as! String)
table_medicine.soldUnitsPlural = (tmp_data_dictionary["sold_units_p"] as! String)
table_medicine.pkgQty = (tmp_data_dictionary["pkg_qty"] as! String)
table_medicine.genericInd = (tmp_data_dictionary["generic_ind"] as! String)
table_medicine.defaultQty = Int64(tmp_data_dictionary["default_qty"] as! String)!
if let tmp_insurances = tmp_data_dictionary["insurance"] as? NSArray
{
do
{
let jsonData = try JSONSerialization.data(withJSONObject: tmp_insurances, options: JSONSerialization.WritingOptions.prettyPrinted)
if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8)
{
table_medicine.insurance = JSONString
}
}
catch
{
print(error)
}
}
//global_appDelegate.saveContext()
do {
try context.save()
} catch {
fatalError("Failure to save context: \(error)")
}
}
}
}

Swift: Saving Firebase data to CoreData in async operation

I am attempting to pull data from Firebase and then save it to CoreData but am having trouble with the async operation. I have a custom function that returns [ConversationStruct] upon completion. I then do a forEach to save it to CoreData.
However, my current implementation saves the object multiple times, ie Firebase have 10 entries, but CoreData would somehow give me 40 over entries which most are repeated. I suspect the problem is in my completionHandler.
//At ViewDidLoad of my VC when I pull the conversations from Firebase
FirebaseClient.shared.getConversationsForCoreData(userUID) { (results, error) in
if let error = error {
print(error)
} else if let results = results {
print(results.count)
let privateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
privateContext.parent = CoreDataManager.shared.persistentContainer.viewContext
results.forEach({ (c) in
let conversation = Conversation(context: privateContext)
conversation.conversationStartTime = c.conversationStartTime
conversation.recipientID = c.recipientID
conversation.shoutoutID = c.shoutoutID
conversation.unreadMessagesCount = Int32(c.unreadMessagesCount!)
conversation.profileImage = c.profileImage
conversation.recipientUsername = c.recipientUsername
})
do {
try privateContext.save()
} catch let error {
print(error)
}
}
}
//At FirebaseClient
func getConversationsForCoreData(_ userUID: String, _ completionHandler: #escaping (_ conversations: [ConversationStruct]?, _ error: Error?) -> Void) {
var conversations = [ConversationStruct]()
ref.child("conversations").child(userUID).observeSingleEvent(of: .value) { (snapshot) in
for snap in snapshot.children {
let snapDatasnapshot = snap as! DataSnapshot
let snapValues = snapDatasnapshot.value as! [String: AnyObject]
let recipientUID = snapDatasnapshot.key
for (key, value) in snapValues {
//Some other logic
self.getUserInfo(recipientUID, { (results, error) in
if let error = error {
print(error.localizedDescription)
} else if let results = results {
let username = results["username"] as! String
let profileImageUrl = results["profileImageUrl"] as! String
URLClient.shared.getImageData(profileImageUrl, { (data, error) in
if let error = error {
print(error.localizedDescription)
} else if let imageData = data {
let convo = ConversationStruct(conversationStartTime: conversationStartTime, shoutoutID: shoutoutID, recipientID: shoutoutID, unreadMessagesCount: unreadMessagesCount, recipientUsername: username, profileImage: imageData)
conversations.append(convo)
}
completionHandler(conversations, nil)
})
}
})
}
}
}
}
struct ConversationStruct {
var conversationStartTime: Double
var shoutoutID: String
var recipientID: String
var unreadMessagesCount: Int?
var recipientUsername: String?
var profileImage: Data?
}
The print statement would print the count as and when the operation completes. This seems to tell me that privateContext is saving the entities when the results are consistently being downloaded which resulted in 40 over entries. Would anyone be able to point me out in the right direction how to resolve this?
Also, the implementation does not persist.

Getting an error when trying to pull data from a JSON Swift [duplicate]

This question already has an answer here:
Json Serialisation Swift 3 type error
(1 answer)
Closed 5 years ago.
So I've been getting an error for a thread 4: SIGABRT on Xcode when trying to parse data from the OpenWeatherApp API. The error that pulls up on the console is:
Could not cast value of type '__NSArrayM' (0x3419714) to 'NSDictionary' (0x3419958)
I looked at different things on this forum already and none of it seems to really work.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var typeCity: UITextField!
var weatherDescription : String = ""
override func viewDidLoad() {
super.viewDidLoad()
// let checkText = typeCity.text
/*
if typeCity?.text == nil{
typeCity.placeholder = "Type a city with no spaces"
let city = "Chicago"
}
*/
let city = "Chicago"
/*
if checkText != nil {
typeCity?.text = city
}
*/
print("City: \(city)")
// Do any additional setup after loading the view, typically from
a nib.
let url = URL(string:
"http://api.openweathermap.org/data/2.5/weather?q=\
(city)&appid=626a124ef0844d2e021329c38a5dfafd")
let task = URLSession.shared.dataTask(with: url!) { (data,
response, error) in
if error != nil{
print(error!)
} else {
if let urlContent = data {
do {
let jsonResult = try
JSONSerialization.jsonObject(with: urlContent, options:
JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(jsonResult)
//let lon = jsonResult["coord"]["lon"].double
//let lat = jsonResult["coord"]["lon"].double
//let temp = jsonResult?["main"]["double"].double
//print
print(jsonResult["name"]!!)
let coordinates = jsonResult["coord"] as! [String:Any]//the coordinates parsing
print("Coordinates: \(coordinates)")
let lon = coordinates["lon"] as! Double
let lat = coordinates["lat"] as! Double
print("Latitude: \(lat) Longitude: \(lon)")
let main = jsonResult["main"] as!
[String:Any]//for the temperature
let kelvin = main["temp"] as! Double
let degreesFahrenheit = 9/5 * (kelvin-273) + 32
print("Temperature: \(degreesFahrenheit)")
let humidity = main["humidity"] as! Double
let pressure = main["pressure"] as! Double
let temp_max = main["temp_max"] as! Double
let temp_min = main["temp_min"] as! Double
let description = jsonResult["weather"]
["description"]as! [String: Any]
print("description")
} catch{
print("Json Processing has failed or city name not recognized.")
}
// let json = jsonResult(data: data)
//print("Lat: \(String(describing: lat)) Lon: \
(String(describing: lon)) Temp: \(String(describing: temp))")
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}}
There seems to be an error with the following line:
let description = jsonResult["weather"]["description"]as! [String: Any]
Thank you in advance for all the help!
You are trying to implicitly convert an array to a dictionary. Try to safely check the type of it:
if let description = jsonResult["weather"]["description"] as? [String] {
[...]
}

issue "unexpectedly found nil" with downloading image from firebase swift

I have an issue when I'm downloading an image from firebase here is my code:
func getuser(){
FIRDatabase.database().reference().child("users").child(userID!).child("credentials").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let name = value?["name"] as! String
let email = value?["email"] as! String
let profilePicLink = value?["profilePicLink"] as? String ?? ""
Variables.userName = name as String
Variables.userEmail = email as String
self.username.text = Variables.userName
print(profilePicLink)
// Create a storage reference from the URL
let storageRef = self.storage.reference(forURL: profilePicLink)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.data(withMaxSize: 1 * 1000 * 1000 ) { (data, error) -> Void in
let pic = UIImage(data: data!)
self.img.image = pic
}
})
}
I'm getting this error:
==> this the link as printed https://firebasestorage.googleapis.com/v0/b/eswitch-72b56.appspot.com/o/usersProfilePics%2FKYe6fIQReNM8Oog4ELOdRLsC99J3?alt=media&token=6f2392bc-d35b-4ebc-b2c1-2dc34bc4b95a
fatal error: unexpectedly found nil while unwrapping an Optional value
The error that I'm getting is in this line
let storageRef = self.storage.reference(forURL: profilePicLink)
below is my snapshot readings:
snap (credentials) {
email = "bilal#me.com";
mobile = 50955514;
name = Bilal;
profilePicLink = "https://firebasestorage.googleapis.com/v0/b/eswitch-72b56.appspot.com/o/usersProfilePics%2FKYe6fIQReNM8Oog4ELOdRLsC99J3?alt=media&token=6f2392bc-d35b-4ebc-b2c1-2dc34bc4b95a";
role = user;
}
Thanks
Here is the solution:
Added this line to func viewDidLoad()
storage = FIRStorage.storage()
You need to make sure that self.storage is initialized.
storage = FIRStorage.storage()

Resources