Database structure in Firebase Swift - ios

I am trying to think of the best solution for storing user's data in firebase, considering scalability, queries etc...
For example: Each user can create a list of categories. I was thinking of storing it like so:
Categories (Collection)
- User ID (Document)
- Category ID
- title
- imageURL
I would like to access the categories collection, find a list of all user ID's and when accessing a user ID I find their list of all the categories they have created...
I am quite new to firebase...
Or should I create Categories(collections) - User ID (document) - Category Id (collection) - title etc (document) ? Just does not seem right....
I appreciate any assistance or tips!
Edit: This is how I am currently saving the data to Firebase from my IOS app:
Model:
struct Category: Codable, Identifiable {
#DocumentID var docId: String?
var id: String? = UUID().uuidString
var authorId: String
var title: String
var categoryImageUrl: String?
Save data function:
guard let userId = Auth.auth().currentUser?.uid else { return } // REMOVE
let db = Firestore.firestore() // REMOVE
let collectionRef = db.collection("Categories").document(userId)
do {
try collectionRef.setData(from: category)
}
catch {
print(error)
}

It's hard to give a comprehensive answer, but one thing to keep in mind is that the client-side SDKs for Firestore don't have an API to get a list of (sub)collection. So it's important that your application can know the collection names without that, either because they are hard-coded (most common) or because they are stored elsewhere (such as in the parent document).

Related

getDocuments() function getting the same data more than one time

I am migrating from relational DB to NOSQL, and I already have read the entire Firebase Documentation, and now I am hands on to a study project to learn more about it.
What I am trying to do is: I have an user_profile where each user created by Auth will get one document.
Inside this document I have an array called groups that shows all groups this user has joined.
I need to retrieve a list of this groups according to the user logged in.
Then I am using getDocuments() to retrieve this information from the DB.
What I need to get is a array of string with the group_id (as per second pic).
The my current code is retrieving the information I need, but it get all the ids as one object. So if I have 4 groups, it will retrieve 4 objects containing the all 4 groups the user has joined.
func getGroups() -> [String] {
let currentUser = Auth.auth().currentUser?.uid
let db = Firestore.firestore()
var groups = [""]
var groupsArray = [""]
db.collection(K.Collections.userProfile)
.whereField(K.DBFields.UserProfile.userId, isEqualTo: currentUser!)
.getDocuments { (snapshot, error) in
if let error = error {
print(error)
} else {
for document in snapshot!.documents {
groups = (document.get("groups")) as! [String]
//groupsArray.append(groups)
print("Group ID: \(groups)")
}
}
}
return groups
}
And the result I am getting is the one below:
roup ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"]
Group ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"]
Group ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"]
Group ID: ["8m0W7cQLuSjQCJes2fpL", "l84GnZSpIUs43cXO13Qm", "unb0LPYOttDN6WogRXDt", "ohG09dwyVrAd6GcXa6mx"]
I have tried so many different approaches to try to fetch the correct data, but hasn't work.
Once again, I just want to get this array as result:
var groups = [
"8m0W7cQLuSjQCJes2fpL",
"l84GnZSpIUs43cXO13Qm",
"unb0LPYOttDN6WogRXDt",
"ohG09dwyVrAd6GcXa6mx"]
Thanks a lot
Leonardo D'Amato
You're retrieving the values from groups as [String] (string array). But then you print that string array as a single value.
If you want to access the individual elements of the [String] in Swift, you can loop over it:
groups = (document.get("groups")) as! [String]
for group in groups {
print("Group ID \(group).")
}
Also see the Swift documentation on accessing array values.
I don't have much idea of Swift but in "node" there were different results when I tried to use for in place of map You should use the following:
let dataDescription = snapshot.data().map(String.init(describing:)) ?? "nil"
print("Document data: (dataDescription)")
Shown here

How to structure a search list in Firestore?

I want to show a list of artists in my app which the user will be able to search through. I'm not sure however how to save this in Firestore?
First I created a collection "searchLists" with a document for each DJ but that means a lot of document reads so that's out of the question.
Now I created a document called "artists" which has a field "artistsDictionary" which contains all the artists.
| searchLists (collection)
* artists (document)
- artistsArray (array)
0: (map)
name: "Artist 0" (string)
1: (map)
name: "Artist 1" (string)
2: (map)
name: "Artist 2" (string)
And I retrieve and parse the array as followed:
let docRef = db.collection("searchLists").document("artists")
docRef.getDocument { (document, error) in
if let document = document, document.exists {
guard let documentData = document.data() else { return }
let artistsDictionaryArray = documentData["artistsArray"] as? [[String: Any]] ?? []
let parsedArtists = artistsDictionaryArray.compactMap {
return SimpleArtist(dictionary: $0)
}
self.artistsArray = parsedArtists
} else {
print("Document does not exist")
}
}
(SimpleArtist is a struct containing a "name" field.)
And I mean, it works, but I'm still new to Firestore and this seems kinda off. Is it? Or is this how I should/could do it?
First I created a collection "searchLists" with a document for each DJ but that means a lot of document reads so that's out of the question.
This is the right approach, so you should go ahead with it.
Why do I say that?
According to the official documentation regarding modeling data in a Cloud Firestore database:
Cloud Firestore is optimized for storing large collections of small documents.
Storing data in an array is not a bad option but this is most likely used, let's say to store favorite djs. I say that because the documents have limits in Firestore. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:
Maximum size for a document: 1 MiB (1,048,576 bytes)
As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much but as your array getts bigger, be careful about this limitation.
First off, Alexs' answer is 100% correct.
I want to add some additional data points that may help you in the long run.
The first item is arrays. Arrays are very challenging in NoSQL databases - while they provide a logical sequence data via the index, 0, 1, 2 they don't behave like an array in code - so for example; Suppose you wanted to insert an item at an index. Well - you can't (*you can but it's not just a simple 'insert' call). Also, you can't target array elements in queries which limits their usefulness. The smallest unit of change in a Firestore array field is the entire field - smaller changes to individual elements of a field can't be made. The fix is to not use arrays and to let FireStore create the documentID's for you data 'objects' on the fly e.g. the 'keys' to the node
The second issue - (which may not be an issue currently) is how the data is being handled. Suppose you release your app and a user has 2 million artists in their collection - with your code as is, all of that data is downloaded at one time which will probably not be the best UI experience but additionally, it could overwhelm the memory of the device. So working in 'chunks' of data it a lot easier on the device, and the user.
So I put together some sample code to help with that.
First a class to store your Artist data in. Just keeps track of the documentID and the artist name.
class ArtistClass {
var docId = ""
var name = ""
init(aDocId: String, aName: String) {
self.docId = aDocId
self.name = aName
}
}
and a class array to keep the artists in. This would be a potential dataSource for a tableView
var artistArray = [ArtistClass]()
This is to write an artist as a document instead of in an array. The documentID is a FireStore generated 'key' that's created for each artist.
func writeArtists() {
let artistsRef = self.db.collection("artists")
let floyd = [
"name": "Pink Floyd"
]
let zep = [
"name": "Led Zeppelin"
]
let who = [
"name": "The Who"
]
artistsRef.addDocument(data: floyd)
artistsRef.addDocument(data: zep)
artistsRef.addDocument(data: who)
}
and then function to read in all artists.
func readArtists() {
let artistsRef = self.db.collection("artists")
artistsRef.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let docId = document.documentID
let name = document.get("name") as! String
let artist = ArtistClass(aDocId: docId, aName: name)
self.artistArray.append(artist)
}
for a in self.artistArray { //prints the artists to console
print(a.docId, a.name)
}
}
}
}
So your data in Firestore looks like this
artists (collection)
8lok0a0ksodPSSKS
name: "Let Zeppelin"
WKkookokopkdokas
name: "The Who"
uh99jkjekkkokoks
name: "Pink Floyd"
so then the cool part. Suppose you have a tableView that shows 10 artists at a time with a down button to see the next 10. Make this change
let artistsRef = self.db.collection("artists").order(by: "name").limit(to: 10)
Oh - and you'll notice the function of sorting now goes the server instead of the device - so if there's a million artists, it's sorted on the server before being delivered to the device which will be significantly faster.
You can also then more easily perform queries for specific artist data and you won't need to be as concerned about storage as each artist is their own document instead of all artists in one.
Hope that helps!

Group chat - How to populate a table view with only the chat rooms a user is involved in?

I'm trying to make a group chat app, and so far I have the app setup (from a tutorial) so that any user can create a room, and all of those rooms show up in a central list, which is the same for all users. Now I need to alter this so that users only see rooms that they created, or have been added to (just like any messaging app like iMessage).
This is how a room is created and appended to the Room object (and uploaded to Firebase):
var rooms: [Room] = []
// Create new room
#IBAction func createRoom(_ sender: Any) {
if let name = newRoomTextField?.text {
let newRoomRef = roomRef.childByAutoId()
let roomItem = [
"name": name
]
newRoomRef.setValue(roomItem)
}
}
private func observeRooms() {
// Observe method to listen for new channels being written to Firebase
roomRefHandle = roomRef.observe(.childAdded, with: { (snapshot) -> Void in
let roomData = snapshot.value as! Dictionary<String, AnyObject>
let id = snapshot.key
if let name = roomData["name"] as! String!, name.characters.count > 0 {
self.rooms.append(Room(id: id, name: name))
self.tableView.reloadData()
} else {
print("Error! Could not decode channel data")
}
})
}
Room object:
internal class Room {
internal let id: String
internal let name: String
init(id: String, name: String) {
self.id = id
self.name = name
}
}
Currently the table view of rooms is populated with cell.textLabel?.text = rooms[(indexPath as NSIndexPath).row].name (cellForRow method), and return rooms.count (numberOfRows method), thus returning all of the rooms in the app, not specific to the user.
This is where I'm not sure how to proceed: I'm guessing I'll add an array "participants: [String]" to my Room object (in addition to "id" and "name"), then in observeRooms when I self.rooms.append(Room(id: id, name: name)), I'll also append the user's id to "participants"? Then populate the table view with only the rooms that the current user's id is a participant in?
Is this the right approach?
Thanks for any help/guidance!
I think this is the right approach, you basically have 2 choices :
Store a participants array into each room, which allow you to query rooms where this array contains our current userId.
Store a rooms array on each user, allowing to query rooms directly from this data.
The first approach seems to make more sense in case you'd like to also display a list of participants, though the query may take longer to execute than the second approach for many rooms * many participants.
You could also use both approach at the same time to have both your functionality and a faster query time for the user rooms.

How to access Data in multipleviewControllers in Swift?

I will try to explain the scenario as best as I can.
Lets say if viewControllerA tells the model to hold some data(for example data from a json response) and then viewControllerD(or any other viewcontroller) needs the data, how do i access the data from the model. Creating an instance of the model in viewControllerD creates a fresh instance without any data.
Below code explains the scenario.
ViewControllerA
let userdetails = UserDetails(json: self.userDetailsList!)
userdetailsarray.append(userdetails) //a global array
//Model
class UserDetails: NSObject {
var name : String?
var profession : String?
var id: String?
init(json: NSDictionary) {
let name = json["name"] as? String
let profession = json["profession"] as? String
let id = json["id"] as? String
self.name = name
self.profession = profession
self.id = id
super.init()
}
}
Possible Solution I know: Creating a global variable
var userdetailsarray = [UserDetails]
and appending UserDetails(model) into this array and using this array across multiple viewControllers. An alternative solution could be the model class being singleton.
I am looking for a more optimistic solution. Thankyou
If the data is more make it encrypted using NSKeyedArchiever and store it in user defaults. Whenever you required the data you can fetch it from user defaults and un archive it using NSKyedUnarchiever.
Note: This is the best solution only if you want to persist the data.

Add only objects that don't currently exist to Realm database

I have a subjects Realm database:
import Foundation
import RealmSwift
class Subjects: Object {
dynamic var subject = ""
override static func primaryKey() -> String? {
return "subject"
}
}
In the database I have four subjects, say, Computing 101, English, History and Geography. Now, I have a struct that can hold an array of subjects, like so:
struct mySubjects {
static var subjects:Array<String> = [String]()
}
The user can add subjects to the struct, say, Computing 101, English, History, Geography and Accountancy for Engineers.
My problem is that I only want to add the one subject that does not already exist in the database, i.e. "Accountancy for Engineers". I thought I could simply code a NSPredicate like so:
for subject in mySubjects.subjects {
let myPredicate:NSPredicate = NSPredicate(format: "subject != '\(subject)'")
results = realm.objects(Subjects).filter(myPredicate)
No luck :(. Any suggestions?
Because you've set up a primary key, you could use the objectForPrimaryKey(_:key:) function.
If the result of something like realm.objectForPrimaryKey(Subject.self, "Subject Name") is nil, then that subject doesn't exist in the database yet.

Resources