Can I get progress while uploading/downloading data from Firebase - ios

I need to show progress while uploading/downloading data from Firebase database.
Can I get it?. I saw in Firebase storage but I didn't saw in Firebase database.

Progress based on data:
I recommend looking over this post, as it will help you understand how to use some of the built in data references to determine your upload progress. While it is not for iOS, it explains the thought process of how you can measure upload progress.
You can always use a progress monitor like such:
let observer = uploadTask.observeStatus(.Progress) { snapshot in
print(snapshot.progress) // NSProgress object
}
Progress based on count:
As to what FrankvanPuffelen said, there is in fact no tool to give you what you are asking for. However, as Jay states, you can determine the "progress" of your task(s) based on how you are reading/writing.
Say for instance you are writing (uploading) 10 photos. For each photo that is successfully written (uploaded), you can simply increment some sort of progress meter by 1/10.
Example for some local file on the users device:
// Some image on the device
let localFile = URL(string: "<PATH>")!
// Make a reference to the file that needs to be uploaded
let riversRef = storageRef.child("chicken.jpg")
// Upload the file to the path you specified
let uploadTask = riversRef.putFile(from: localFile, metadata: nil) { metadata, error in
if let error = error {
// Whoops, something went wrong :(
}
else {
// Tada! Uploaded file
someCountVariable += 1
// If needed / applicable
// let downloadURL = metadata!.downloadURL()
}
}
Of course for one (1) item you will go from 0 to 100 (..real quick - shout out to Drake), but this can just be nested in some loop that iterates through some list of items to upload. Then per each item successfully uploaded increment.
This will be more expensive in terms of requests if you need to upload objects other than multiple images, but for the purpose this will give you a nice visual / numeric way of tracking at least how many items are left to be successfully uploaded.
If you really want to get down to the nitty gritty, you can always try checking the current connection strength to determine a relative data transfer speed, and cross that with the current task you are attempting. This could potentially give you at least some estimate the progress based on how long the process should take to how long it actually has taken.
Hope some of this helps / points you in the right direction. Happy coding!

val bytesTransferred=taskSnapshot.bytesTransferred.toFloat()
val totalByteCount=taskSnapshot.totalByteCount.toFloat()
val progress = bytesTransferred / totalByteCount
Log.d("progress", (progress*100).toString())
Log.d("progressDivide", (bytesTransferred / totalByteCount).toString())
Log.d("progressbytesTransferred", bytesTransferred.toString())
Log.d("progresstotalByteCount", totalByteCount.toString())

Related

Cloud Storage for Firebase bandwidth quota exceeded after downloading multiple images – what are the best practices?

I'm building an iOS app with data stored in Firestore and associated images stored in Cloud Storage for Firebase. Essentially, my app's starting screen is a scrolling list of cards representing documents from one of my Firestore collections and an associated image (pulled from Cloud Storage, matched using document title).
To generate the starting scrollview data, each time I open my app, I run a call to both Firestore and Cloud Storage to populate my local structs and retrieve the images (around 10 of them, each 600px by 400 px and between 300-500 KB).
I'm currently on the free Spark plan. My quotas are fine for Firestore, but yesterday, I was testing the app in the simulator (maybe ran it ~60 times?) and exceeded the 1 GB bandwidth limit for Cloud Storage calls. I think this is because I'm downloading a lot of images and not storing them locally (because I download them again each time the app is run?).
Here's the code that runs each time the app is opened to get all of the data and associated images:
// StoryRepository.swift
import Foundation
import FirebaseFirestore
import FirebaseStorage
import FirebaseFirestoreSwift
class StoryRepository: ObservableObject { // listen to any updates
let db = Firestore.firestore()
let storage = Storage.storage()
#Published var stories = [Story]() // Story is a local struct with vars for title, previewImage (the associated card image I get from Cloud Storage), and other data in Firestore.
init() {
loadStoryCardData()
}
func loadStoryCardData() {
db.collection("stories").addSnapshotListener{ (querySnapshot, error) in
guard let documents = querySnapshot?.documents else {
print("Error fetching documents: \(error!)")
return
}
// create Cloud Storage reference
let storageRef = self.storage.reference()
// each document represents a story. get the title of that story, and then fetch the image from Cloud Storage that has the same filename as that title.
documents.forEach { document in
guard let title = document["title"] as? String else {
print("Could not read story title.")
return
}
let primaryImageRef = storageRef.child("storyCardPrimaryImages/" + title + ".jpg")
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
primaryImageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print("Error fetching primary image: \(error)")
return
} else {
if let image = UIImage(data: data!) {
self.stories.append(Story(title: title, previewImage: image, data: document.data())!)
}
}
}
}
}
}
}
I'm just wondering how to optimize this, because when I release this app and gain users, I can just imagine where my bandwidth will go. Should I store the local structs and images in the iOS FileManager so I only download them once, when the app first runs? How will I update the local data should anything in either Firestore or Cloud Storage change?
I thought about storing the images in a smaller size, but I really need them to be 600px by 400px in my app. I'd prefer to stay on the free Spark plan if possible. Honestly, I'm just a little taken aback that I've already run into quota limits while testing – when it's just me running the app. Overall, I'm at a bit of a loss – any advice would be really, really appreciated.
Should I store the local structs and images in the iOS FileManager so I only download them once, when the app first runs?
Using a local cache like this is definitely one good way to reduce the total bandwidth use.
How will I update the local data should anything in either Firestore or Cloud Storage change?
That will be for you to engineer. In general, one way to do this would be for your process that updates the objects in storage to also update some metadata about the image, and store that in Firestore. Your app can query Firestore, for changes, then check to see if your locally cached files are up to date before downloading them again.
I thought about storing the images in a smaller size, but I really need them to be 600px by 400px in my app.
You can always increase the amount of JPG compression, which will also reduce the quality of the image. That's a tradeoff you'll have to experiment with to your preference.

Storage downloading costs too much for Firestore app in Swift 4

So the way my app is working is kind of like instagram. A user can upload a photo, and whenever someone loads the app it downloads each picture that was uploaded from the firebase.
I understand that I need to buy space or change my plan, but I didn't do that much and I'm wasting 1.7gb from a user in like an hour. Each photo costs like 17mb to upload and download.
I am not sure what I can do to lessen my downloading here.
The way I download from firestore is like this from the f:
// Create a reference to the file you want to download
let islandRef = storageRef.child("images/island.jpg")
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
islandRef.getData(maxSize: 1 * 10240 * 10240) { data, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// Data for "images/island.jpg" is returned
let image = UIImage(data: data!)
}
}
And each time it loads a photo into a collectionviewcontroller. Which means it is like 17mb for each photo which is a lot. Any suggestions? Thanks
So this is where you want to make a decision about the level of quality for the photos that you upload to firebase. I can assure you that instagram and any other social media platform only store versions of your pictures that are compressed and optimized for size.
You can easily compress your image by doing something like this
let data = imageToUpload.jpegData(compressionQuality: 0.3)
you would then upload that new compressed version of the image to firebase and dramatically improve your storage efficiency.

Firebase does not update values throughout an app until the user is logged out

So, I have been coming across a problem where my Firebase app does not update user values when a user makes an update. To be more clear: Lets say user 1 has a photo of a dog and then changes it to a cat.
Once they change it to a cat, my node value in Firebase is successfully updated but the user themselves won't be able to see the change in other previously loaded areas in the app (other places with the dog picture) until they log out and then log back in.
For this reason I was wondering if there was any way to conduct a background app refresh that way all previous dog values in the app are changed to cat values without the user having to log out and then log back in. Please note that this same problem occurs not only with my user's profile picture but also any other user field I have setup.
Here is how I am updating a node value for my user in Firebase:
let storageRef = FIRStorage.storage().reference()
_ = FIRStorageMetadata()
let filePath = "\(FIRAuth.auth()?.currentUser?.uid)/\("userPhoto")"
let profileImageData = UIImageJPEGRepresentation(self.profilePicture.image!, 1.0)
if let data = profileImageData {
storageRef.child(filePath).put(data, metadata: nil){(metaData,error) in
if let error = error {
print(error.localizedDescription)
return
} else {
let downloadURL = metaData!.downloadURL()!.absoluteString
let userPhotoUpdateRef = FIRDatabase.database().reference().child("users").child(self.currentUser).child("userPhoto")
userPhotoUpdateRef.setValue(downloadURL)
}
}
}
If you have any questions please ask! Any help would be appreciated!
The Firebase SDK for Cloud Storage provides an easy way to read file from and write files to cloud storage. It does not provide a way to monitor those files.
The easiest way to provide a monitoring approach is to write the metadata of the files to the Firebase Realtime Database. See this short section in the Storage docs for a brief mention of that: https://firebase.google.com/docs/storage/ios/file-metadata#custom_metadata
When you write data to a location in the Firebase Database, all apps that are actively monitoring that location will be instantly updated. When they get that update, you can reload the image from Cloud Storage for Firebase.

Disable confirmation on delete request in PHPhotoLibrary

What I am trying to do is to save videos to PHPhotoLibrary, and then remove them when upload to clients remote server in the application completes (basically, photo library serves as temporary storage to add additional layer of security in case anything at all fails (I already save my vides it in the applications directory).
Problem:
The problem is for that to work, everything has to work without input from the user. You can write video to photos library like this:
func storeVideoToLibraryForUpload(upload : SMUpload) {
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.Authorized {
// Don't write to library since this is disallowed by user
return
}
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
// Write asset
let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(fileURLWithPath: upload.nonsecureFilePath!)!)
let assetPlaceholder = assetRequest.placeholderForCreatedAsset
let localIdentifier = assetPlaceholder.localIdentifier
// Store local identifier for later use
upload.localAssetIdentifier = localIdentifier
}, completionHandler: { (success, error) -> Void in
....
})
}
And that works flawlessly, I get local identifier, I store it for later use.. Unicorns and rainbows.
Now when I want to remove that video immediately after upload finishes, I call following:
func removeVideoFromLibraryForUpload(upload : SMUpload) {
// Only proceed if there is asset identifier (video previously stored)
if let assetIdentifier = upload.localAssetIdentifier {
// Find asset that we previously stored
let assets = PHAsset.fetchAssetsWithLocalIdentifiers([assetIdentifier], options: PHFetchOptions())
// Fetch asset, if found, delete it
if let fetchedAssets = assets.firstObject as? PHAsset {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
// Delete asset
PHAssetChangeRequest.deleteAssets([fetchedAssets])
}, completionHandler: { (success, error) -> Void in
...
})
}
}
}
Which successfully deletes the video, BUT user have to confirm deletion first. That is a problem as that backing up won't work.
I obviously know why there is confirmation (so you don't clear entire user library for example, but the thing is, My app made the video - and so I thought there will be way around it, since as an "owner" I should not be doing that, or at least have option to disable confirmation.
Thanks in advance!
TLDR: How can I disable confirmation on delete request, if my application created that content? (I don't want to delete anything else).
Note: Somebody can probably say this is rather strange thing to do but the application is distributed internally and there is good reason to do it like this (the video content is too valuable to be lost, even if user deletes the application for some reason, or there is anything at all that goes wrong, we need to be able to preserve the videos), so please don't question that and just focus your attention on the question :)
I cannot see a way to avoid the delete confirmation. It is an implementation detail of the Photos framework, similar to the way you cannot prevent the device from asking the user's permission to use the microphone when your app tries to use it, and is a matter of security & trust. Once you have saved an asset to the device photo library your app is no longer the owner of that asset, so as you noted in your question the device must of course ensure the app has the user's permission before it goes about deleting such data.
You can never entirely safeguard your users' data against their own unpredictable behaviour - if they decide to remove your app, or delete a particular asset from within Photos, it is up to them. I think your best option is to either put up with the built-in delete confirmation, or to provide a guide to your users that makes it clear that they should be careful to protect this important data by backing up their device, and not deleting the app!
If you did decide to stick to this approach, perhaps the best thing you could do is to prepare the user for the fact that their device may ask them for confirmation to delete a file that is being uploaded to your own servers. For example, put up your own modal alert just before trying to delete the asset. I wouldn't normally suggest that kind of approach for a public shipping app, but since you're only distributing internally it may be acceptable for your team.

Cloudkit fetch data (strings and image asset) take a long time to appear after call

I was hoping that someone can help a coding newbie with what might be considered a stupid question. I'm making a blog type app for a community organization and it's pretty basic. It'll have tabs where each tab may be weekly updates, a table view with past updates and a tab with general information.
I setup cloudkit to store strings and pictures, and then created a fetchData method to query cloud kit. In terms of the code (sample below) it works and gets the data/picture. My problem is that it takes almost 5-10 seconds before the text and image update when I run the app. I'm wondering if that's normal, and I should just add an activity overlay for 10 seconds, or is there a way to decrease the time it takes to update.
override func viewDidLoad() {
fetchUpcoming()
}
func fetchUpcoming() {
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase
let query = CKQuery(recordType: "Upcoming", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
publicData.performQuery(query, inZoneWithID: nil) { results, error in
if error == nil { // There is no error
println(results)
for entry in results {
self.articleTitle.text = entry["Title"] as? String
self.articleBody.text = entry["Description"] as? String
let imageAsset: CKAsset = entry["CoverPhoto"] as! CKAsset
self.articlePicture.image = UIImage(contentsOfFile: imageAsset.fileURL.path!)
self.articleBody.sizeToFit()
self.articleBody.textAlignment = NSTextAlignment.Justified
self.articleTitle.adjustsFontSizeToFitWidth = true
}
}
else {
println(error)
}
}
}
Another question I had is about string content being stored on cloud kit. If I want to add multiple paragraphs to a blood entry (for example), is there a way to put it in one record, or do I have to separate the blog entry content into separate paragraphs? I may be mistaken but it seems like CloudKit records don't recognize line breaks. If you can help answer my questions, I'd be really appreciative.
It looks like you might be issuing a query after creating the data, which isn't necessary. When you save data, as soon as your completion block succeeds (with no errors) then you can be sure the data is stored on the server and you can go ahead and render it to the user.
For example, let's say you're using a CKModifyRecordsOperation to save the data and you assign a block of code to the modifyRecordsCompletionBlock property. As soon as that block runs and no errors are passed in, then you can render your data and images to your user. You have the data (strings, images, etc.) locally because you just sent them to the server, so there's no need to go request them again.
This provides a quicker experience for the user and reduces the amount of network requests and battery you're using on their device.
If you are just issuing normal queries when your app boots up, then that amount of time does seem long but there can be a lot of factors: your local network, the size of the image you're downloading, etc. so it's hard to say without more information.
Regarding the storage of paragraphs of text, you should consider using a CKAsset. Here is a quote from the CKRecord's documentation about string data:
Use strings to store relatively small amounts of text. Although
strings themselves can be any length, you should use an asset to store
large amounts of text.
You'll need to make sure you're properly storing and rendering line break characters between the user input and what you send to CloudKit.

Resources