Random profile images in Firebase - swift

I would like to know, what would be the best approach for something like identicons and gravatar but in swift and to store them into Firebase and assign as profileImage.
Other thing I came up with, would be to make array of image url-s and while registering, randomly pick one url and store it. Is it OK to do?
Can you guys lead me to right direction?

I wouldn't recommend you holding all this data at once especially if you'll end up having multiple images. I would recommend one of the following
A single gravatar
Simply assign all newbie's profile image to this. You can even go ahead and store it inside your app so no need for a Firebase call or memory hogs.
Feature dependent
Try assigning gravatars based on specific fixed/factual characteristics of your users. Sex is the most preferable since its binary. Facebook uses the same approach when no profile image is added by the user. Should you opt for this method, Firebase is not really needed.
Dynamic
Multiple default avatars available and open to further addition. My guess is that you're interested in this one.
User selection
Have all of your avatars stored in Firebase and simply have the user select the most appealing to them.
Since you'll be adding more avatars in the future, then I'd advice against loading all of your avatars at once. For memory optimization,
Query for the first 10 avatars and present them to the user
If user hasn't selected avatar and let's say they are 3/4 through of the already loaded images, query for another 10 avatars.
This method is very memory friendly and it will not overwhelm the user with so much information.
Random selection
Have your avatars stored in Firebase Storage but also include a node in your Firebase database which tracks the number of avatars available. For this case lets call it total. Along with this I'd have a number-string to avatar url mapping in my avatar node. My structure would be something similar to this
{
"avatars":
{
"total":10,
"1":"https://www.google.com",
"2":"https://www.facebook.com",
"3":"https://www.youtube.com"
}
}
This begs the question, How do I randomly query for avatars?
Uhh huuh..
Query Firebase to get the number of avatars you currently have
Generate a random integer; say x which adheres to 1<=x<=total
Convert x to String; y = String(x)
Query Firebase where avatars.id == y and voila you have the imageURL
Download the image from imageURL
The downside to this is that the user has no control and should they hate their avatar there's nothing they can do about it.
The upside is that at any given moment you're NOT loading all of the images in memory.

Related

Create anonymous highscorelistor in unity

I have simple game on Google Play and Apple App Store that I have created in Unity. Now I am thinking about creating a global high score, so if you are in top 10 for example you can enter a made up user name.
The thing is I do not want the users to sign in since I want the game to be easy to access. One user can also enter a new username each time a record is beaten. So you can for example have the first three positions.
In principle, the app should read the high score from database and one should be able to write to it with {userID, new_score}, but no data is connected to any particular user. I am used to work with Firebase, what would be the easiest way to achieve this?
Thanks,
Erik
Well, the fact that you don't have data connected to any particular user do not means that you can't store the highscores. Cause one user can beat himself.
So if your database (I don't know if it's realtime or firestore) knows which are the top scores, when a user (indpendently of his ID) beats the score threshold, you can give him the option to put a name on that highscore, and update your database with this new score.
I will also recommend to bind users with data, his highscore or the name it's using to register the highscore, but you can achieve the same result without it.
You could try having a read and create only set of records, where write requires a valid anonymous auth key. I haven't tried that setup with Anonymous only auth before though.
Otherwise you could also try setting up a cloud function to validate and write the high score records.

Swift Firebase displaying from the database to a label.text in a tableview

I'm going to try my best to explain the question as it is a bit confusing. I'm creating a restaurant rating app for its users to rate restaurants(on a 1 to 5-star rating scale). I will be keeping the users votes on Firebase then calculate the average rating for that particular restaurant and display it in my app.
To summarize, in my main tableview there will be restaurant names and next to them their ratings, which are kept by a UIImage(star icon) and a label(to show their ratings). I have already created a comments section for each restaurant by keeping track with a parameter which cell(restaurant) the user has clicked on and based on that, that restaurant's node is created or updated in the firebase database and can show the comments specific to that restaurant in the app without a problem. When a restaurant is clicked, users are directed to that restaurant's comments section.
Now I'm also thinking about storing the ratings on the same nodes for each restaurant. As I said earlier, I want to store the overall ratings just next to the restaurants, in a label, before reaching to the comments section. My question is that after I store the ratings on my database, how will I actually manage to display the correct ratings to the labels? I mean I kept track of which comments to display in my comments section by creating a parameter to track which cell was clicked but now, I want my app to display the restaurant ratings directly, before the user clicks on the restaurant.
Is this possible? If yes, how can I manage this? I hope I explained things clearly. I'd appreciate any help. Thank you very much for reading my long post!
this question is a little bit outside the scope of how questions should be asked here, but I'll do my best to give you some advice here!
What you're looking is absolutely possible, and honestly a great place to start for an iOS project.
1: Firebase database is a great tool, but if you're starting a new project, you should really be using Firebase Firestore. Firebase Database is being deprecated (i.e. they're going to cut of support for it at some point), and Firestore is generally easier for to get the hang of.
2: A common approach to do what you're trying to do with Firestore is to have one document contain all the data about each restaurant, and then for each restaurant to contain a collection of all the comments.
3: Firebase also has a feature called Functions, which you should absolutely check out. Functions can run whenever something "happens" in Firestore. Thus it's actually pretty easy to create a function that whenever a new review/comment for a restaurant is created it collects all the reviews, creates an average, and then would update your restaurant with that average score.
So here's a rough idea of a data structure that works:
[restaurants]
-> {restaurantId}
- about
- name
- blahBlahBlah
- averageRating
- [reviews]
-> {reviewId}
- text
- rating
Happy coding!
It should be fairly straightforward:
First model your rating system; Second keep instances of those
rating;
create a method within your Restaurants class that returns the current rating based on the result of your rating system.
** hint: (What this method returns is what you display on your label.) ***
Finally, In your tableview/collectionView label you should just be calling that method. If you do everything correctly. The tableview will collect that data and display regardless of any changes you make to your rating system in the future.
To be more explicit:
load your restaurants up to firebase.
when a user rates a Restaurant, have a method within the Restaurant class to handle such requests. e.g. currentRestaurant.updateRating()
Load the changes up to the firebase database.
in tableview/CollectionView call something like currentRestaurant.retreiveRating()
Also, your dataManager should be handling any uploads/downloads not the actual Restaurant object.

How can I store images and strings locally on Swift

I'm making an app where the user can take a picture and add a title and description. But now I need to store the picture and the titles. I’ve tried making an object that contains title, description and image properties.
When it’s done I save an array of custom objects with the information on it with UserDefaults. My idea is showing in another view a table view with all the content and pictures the user has taken on the cells. I tried getting back the information with user defaults. It was working well until the user saves too many pictures. When the viewController with the tableview loads, then my app gets slower, and eventually it crashes.
I suppose the problem is when I load all the array with all custom objects, all the pictures are loaded into memory although they aren’t used and displayed for users. So I think it isn’t the best way to make what I want to make.
Is there any way better to make what I’m making or store data more efficiently and use it in tableview without using all the memory of the device?
I’m making the app from 0 again.
Can you show me how to store data and images efficiently?
There are several options for permanently storing user data on the device such that it will survive app and phone restarts. NSUserDefaults typically is for small amounts of data, such as user preferences.
When it comes time to store a lot of data, in particular big binary objects like photos, you need to decide which design you want to use. One option:
store all the photos in one directory in your apps documents and then use some simple lookup store (perhaps Core Data, or SQLite, or even a flat file) to index the photos and their metadata. Or if you don't care about indexing you can read the list of file names from the directory and sort them by time.
The other problem you are maybe having is that you are trying to load all of the photos at once for the user. As you have discovered, once you have more than a handful of photos this system falls apart. You need some mechanism to load only a few photos at a time, preferably only the ones the user needs to be displayed at that moment.
So, for example, if you are displaying the photos in a tableview, you want to only load the 10-15 photos that are visible in the table at any given time.
When storing this kind of data, you have 2 options (In fact you have more than 3 options, but saving image to the disk is IMHO complicated for this.) -
UserDefaults
Database like CoreData/Realm/FireBase...
The first one is recommended when there is not much data to save... For operating with more data, I would use database - CoreData...
For you operation you can use CoreData and NSFetchedResultsController (which is designed especially for fetching objects from the database)...
you can read the FetchedResultsController doc here... and core data basics here
Wish you best luck!

Persistent storage of object data

i am developing an sample app where i have N number of custom button with images present in the screen.I can able to perform following operation(drag drop,rotate,change color etc.)upon these buttons.
I can also able to draw line on the screen.
So now i want to persist these data so that on restart of application i can able to see my previous objects.What is the best solution to do this?
You've got three options, all pretty simple:
store the data in user defaults
store the data in a plist in the documents directory
use core data
In all cases you'll need to transform your data into property list type format (strings, numbers, data, dictionaries, arrays). User defaults would be frowned on if you had a large amount of data. Core data could be overkill. A plist is probably your best bet on the information you have given.
You'll need to store your data either each time it changes or when the application goes into the background, and restore it if needed on launch. I say if needed, because if you are just resuming from background, everything may still be present if iOS hasn't sent you any memory warnings or closed you down.
Core Data. Use a managed object to represent each button with attributes like "color","position_x", "position_y", etc.. use sqlite as the persistent store.

Should loaded images and text be stored in memory or retrieved each time

My app has various pins that drop onto a map and when you click on the pins you get more information about this entity.
Each time you click on the entity it retrieves the information from a web service. Should I only retrieve this information once and store it in memory or should I retrieve it each time that page loads?
It's a small about of text and 3 small images?
If its just 3 small images and some text that will not change i would probably cache them in the application instead of retriving them over and over, it will provide a better user expirience in my opinion...
Also I have a Core Data application that uses images...
I tried both methods but I chose to retrieve it every time because for my goal is the best practice. However this method causes me to write some code and a lot of if and else!
How Daniel said, cache each image can be a better solution for your problem because if an user would like to retrieve these images from internet but the connections isn't fast, he'll wait a lot of time...