Query two Cloud Firestore collections into 1 object - swift

I am using Flamelink as a headless CMS integrated with Firebase. All of my string fields are working just fine; I am just having a little trouble getting a URL of the media that was uploaded to Firebase Storage.
The collection that I'm getting my string fields from is fl_content
The fields are:
string1
string2
imageUpload
Within Firebase, I can see the data that gets saved from Flamelink:
string1: "Titanium Tee"
string2: "Lower your handicap by 50 with premium Titanium golf tees!"
imageUpload returns an array with a reference to fl_files (a different collection in Firebase)
imageUpload:
0 fl_files/ZqVeXI3vX0rFDuJVDzuR
Under fl_files > ZqVeXI3vX0rFDuJVDzuR, I'm able to see the full filename of the image I upload; the documents in fl_files have a file field. I need to get this filename sent to my object so that I'm able to use the images in my UI.
Here's my progress:
Task:
struct Task{
var string1:String
var string2:String
//var imageUpload:String
var counter:Int
var id: String
var dictionary: [String: Any] {
return [
"string1": string1,
"string2": string2,
//"imageUpload": imageUpload,
"counter": counter
]
}
}
extension Task{
init?(dictionary: [String : Any], id: String) {
guard let string1 = dictionary["string1"] as? String,
let string2 = dictionary["string2"] as? String,
//let imageUpload = dictionary["imageUpload"] as? String,
let counter = dictionary["counter"] as? Int
else { return nil }
self.init(string1:string1, string2: string2, /*imageUpload: imageUpload,*/ counter: counter, id: id)
}
}
VC:
private var documents: [DocumentSnapshot] = []
public var tasks: [Task] = []
private var listener : ListenerRegistration!
fileprivate func baseQuery() -> Query {
return Firestore.firestore().collection("fl_content").limit(to: 50)
}
fileprivate var query: Query? {
didSet {
if let listener = listener {
listener.remove()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.query = baseQuery()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.listener.remove()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
listenerStuff()
}
func listenerStuff(){
self.listener = query?.addSnapshotListener { (documents, error) in
guard let snapshot = documents else {
print("Error fetching documents results: \(error!)")
return
}
let results = snapshot.documents.map { (document) -> Task in
if let task = Task(dictionary: document.data(), id: document.documentID) {
return task
}
else {
fatalError("Unable to initialize type \(Task.self) with dictionary \(document.data())")
}
}
self.tasks = results
self.documents = snapshot.documents
self.databaseTableView.reloadData()
}
}
How do I query fl_files so that I can populate the imageUpload property of Task with the URL of the uploaded image? Do I do another separate query? Or can I access fl_files from baseQuery()?
EDIT
Here's my attempt at getting to fl_files from fl_content. Trying to just simply populate 2 text fields an an image field (in a UITableViewCell) from Firebase. Is property what I need in getdocument?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "databaseCellID", for: indexPath) as! DatabaseTableViewCell
let item = tasks[indexPath.row]
cell.label1.text = item.string1
cell.label2.text = item.string2
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
print("PARENT \(docRef.parent)")
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload")
// TODO - use this property to get to fl_files?
print("Document data: \(property!)")
}
}
}

You will need to perform a separate query. There are no SQL-like join operations in Firestore, and references are not automatically followed.

The simplest way to do this is probably to write a small Cloud Function that will respond to the uploaded files and place the image URL automatically in your desired collection, allowing for easier querying.
We will attach an onCreate listener to the fl_files collection, then write the downloadURL to the corresponding fl_content document when we see that a new file was created. Note that your actual content fields may be different from the examples I have used here (I'm not personally familiar with Flamelink).
/**
* Firebase Cloud Function deployed to your same project.
*/
const functions = require('firebase-functions');
import { firestore } from 'firebase-admin';
exports.newFileUploaded = functions.firestore
.document('fl_files/{newFileID}')
.onCreate((snap, context) => {
const fileID = context.params.newFileID;
const fileData = snap.data();
// get whatever data you want out of fileData..
// (let's assume there is a downloadURL property with the URL of the image)
const downloadURL = fileData.downloadURL;
// write that download URL to the corresponding fl_content document
// (assuming the file ID is the same as the content ID)
const contentRef = firestore().collection('fl_content').doc(fileID);
// update the imageURL property, returning the Promise, so the function
// does not terminate too early
const updateData = { imageUpload: downloadURL };
return contentRef.update(updateData);
});
Now, you are able to perform just 1 query to fl_content and the new file's image URL will be included in the request. The tradeoff we make is we have to perform 1 additional Firestore write to save 50% on all future read requests.
Alternatively, without a Cloud Function, we would have to perform 2 separate queries to get the content from fl_content and fl_files. As Doug mentioned, it is not possible to perform a JOIN-like query using Firestore due to the scalable way that it was designed.

Related

How I can add pagination in swift?

I have spend so much time to find a solution with the documentation from Firebase without any success. I using Swift 5.3 and Firestore and have the following code:
func readFlights() {
Spinner.startAnimating()
let myquery = db.collection("flight").limit(to: 25).whereField("Userid", isEqualTo: userID).order(by: "DateDB", descending: true)
.order(by: "Start", descending: true)
myquery.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
self.prefixArray.append(document.get("Prefix") as! String)
self.dateArray.append(document.get("Date") as! String)
self.startArray.append(document.get("Start") as! String)
self.stopArray.append(document.get("Stop") as! String)
self.landingArray.append(document.get("Landing") as! String)
self.takeOffArray.append(document.get("Takeoff") as! String)
self.flightTimeArray.append(document.get("FlightTime") as! String)
self.engineTimeArray.append(document.get("EngineTime") as! String)
self.idArray.append(document.get("id") as! String)
self.destinationArray.append(document.get("Destination") as! String)
self.originArray.append(document.get("Origin") as! String)
self.informationArray.append(document.get("Addinfo") as! String)
self.rulesArray.append(document.get("VFRIFR") as! Int)
self.pilotCopilotArray.append(document.get("PilotoCopiloto") as! Int)
self.engineArray.append(document.get("MnteMlte") as! Int)
self.dayNightArray.append(document.get("DayNight") as! Int)
}
DispatchQueue.main.async{
self.tabelView.reloadData()
self.Spinner.stopAnimating()
}
}
}
working fine but I need to include in this code pagination. That means when I received the first 25 records from Firestore and I slip down in the list with my finger so I want after the latest record he load 25 records more and show them.
I would appreciate your help. Thank you
First, create a document cursor that is an instance property of the view/view controller:
var cursor: DocumentSnapshot?
let pageSize = 25 // for convenience
Second, apply the page size to the query:
let myquery = db.collection("flight").limit(to: pageSize).whereField("Userid", isEqualTo: userID).order(by: "DateDB", descending: true).order(by: "Start", descending: true)
Third, whenever you receive a snapshot from Firestore, update the cursor at some point in the return (ideally, after you've unwrapped the snapshot and before you've parsed the documents):
func getData() {
myquery.getDocuments(completion: { (snapshot, error) in
...
if snapshot.count < pageSize {
/* this return had less than 25 documents, therefore
there are no more possible documents to fetch and
thus there is no cursor */
self.cursor = nil
} else {
/* this return had at least 25 documents, therefore
there may be more documents to fetch which makes
the last document in this snapshot the cursor */
self.cursor = snapshot.documents.last
}
...
})
}
Finally, whenever the user scrolls to the bottom, fetch another page using the cursor:
func continueData() {
guard let cursor = cursor else {
return // no cursor, exit
}
myquery.start(afterDocument: cursor).getDocuments(completion: { (snapshot, error) in
...
// always update the cursor whenever Firestore returns
if snapshot.count < self.pageSize {
self.cursor = nil
} else {
self.cursor = snapshot.documents.last
}
...
})
}
For a fluid user experience, you will need to greatly refine this code, but this is the foundation from which you can paginate Firestore. You can also paginate in Firestore using a document offset (instead of a document cursor) but this is to be avoided (refer to documentation for the reasons).
You can use awesome solution from: https://github.com/pronebird/UIScrollView-InfiniteScroll
For your example:
tableView.addInfiniteScroll { (tableView) -> Void in
readFlights("offset if need")
tableView.finishInfiniteScroll()
}
By using UITableViewDelegate, u can call the function. Each time when you scroll to the bottom, it will check the max of your limit and if the condition is true, then fetch data again.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastItem = self.array.count - 1
if indexPath.row == lastItem {
if limit < max_limit {
limit += 25
//Get data from Server
readFlights(limit:Int)
}
}
}
The max_limit means the total amount of limits, usually, it returned by server in meta

Firestore Listener Document Changes

I am trying to create a Listener for changes to a Document. When I change the data in Firestore (server) it doesn't update in the TableView (App). The TableView only updates when I reopen the App or ViewController.
I have been able to set this up for a Query Snapshot but not for a Document Snapshot.
Can anyone look at the code below to see why this is not updating in realtime?
override func viewDidAppear(_ animated: Bool) {
var newDocIDString = newDocID ?? ""
detaliPartNumberListerner = firestore.collection(PARTINFO_REF).document(newDocIDString).addSnapshotListener { documentSnapshot, error in
guard let document = documentSnapshot else {
print("Error fetching document: \(error!)")
return
}
guard let data = document.data() else {
print("Document data was empty.")
return
}
print("Current data: \(data)")
self.partInfos.removeAll()
self.partInfos = PartInfo.parseData2(snapshot: documentSnapshot)
self.issueTableView.reloadData()
}
In my PartInfo file
class func parseData2(snapshot: DocumentSnapshot?) -> [PartInfo] {
var partNumbers = [PartInfo]()
guard let snap = snapshot else { return partNumbers }
//for document in snap.documents {
// let data = document.data()
let area = snapshot?[AREA] as? String ?? "Not Known"
let count = snapshot?[COUNT] as? Int ?? 0
//let documentId = document.documentID
let documentId = snapshot?.documentID ?? ""
let newPartInfo = PartInfo(area: area, count: count, documentId: documentId)
partNumbers.append(newPartInfo)
return partNumbers
}
UI work must always be done on the main thread. So instead of your last line in your first code snippet, do this:
DispatchQueue.main.async {
self.issueTableView.reloadData()
}
I think this might be the solution to your problem. (A little late, I know ...)

How to sort data in Firebase?

I'm now able to sort posts and users by time.
My data structure looks like that:
posts
-postId
imageRatio:
imageUrl:
postText:
postTime:
uId:
users
-UserId
email:
profileImageURL:
radius:
uid:
username:
username_lowercase:
UPDATE
Now, I created a new class with all datas for the user and the posts:
class UserPostModel {
var post: PostModel?
var user: UserModel?
init(post: PostModel, user: UserModel) {
self.post = post
self.user = user
}
}
Declaration of my post array:
var postArray = [UserPostModel]()
Here, Im loading the datas into the new class:
self.observeRadius(completion: { (radius) in
let currentRadius = radius
// Üperprüfe, welche Posts im Umkreis erstellt wurden
let circleQuery = geoRef.query(at: location!, withRadius: Double(currentRadius)!)
circleQuery.observe(.keyEntered, with: { (postIds, location) in
self.observePost(withPostId: postIds, completion: { (posts) in
guard let userUid = posts.uid else { return }
self.observeUser(uid: userUid, completion: { (users) in
let postArray = UserPostModel(post: posts, user: users)
self.postArray.append(postArray)
print(postArray.post!.postText!, postArray.user!.username!)
self.postArray.sort(by: {$0.post!.secondsFrom1970! > $1.post!.secondsFrom1970!})
})
})
Here I'm loading the datas into the table view cells:
extension DiscoveryViewController: UITableViewDataSource {
// wie viele Zellen
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(postArray.count)
return postArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DiscoveryCollectionViewCell", for: indexPath) as! DiscoveryCollectionViewCell
cell.user = postArray[indexPath.row]
cell.post = postArray[indexPath.row]
//cell.delegate = self
return cell
}
}
Thanks in advance for your help!
There's a lot of code in the question and sometimes, simpler is better. So let's take a Post class, load the posts, get the associated user name and store it in an array. Then when complete, sort and print the posts in reverse chronological order.
A class to hold the post data and the user name
class PostClass {
var post = ""
var timestamp: Int! //using an int for simplicity in this answer
var user_name = ""
init(aPost: String, aUserName: String, aTimestamp: Int) {
self.post = aPost
self.user_name = aUserName
self.timestamp = aTimestamp
}
}
Note that if we want to have have both post data and user data we could do this
class PostUserClass {
var post: PostClass()
var user: UserClass()
}
but we're keeping it simple for this answer.
Then an array to store the posts
var postArray = [PostClass]()
and finally the code to load in all of the posts, get the associated user name (or user object in a full example).
let postsRef = self.ref.child("posts")
let usersRef = self.ref.child("users")
postsRef.observeSingleEvent(of: .value, with: { snapshot in
let lastSnapIndex = snapshot.childrenCount
var index = 0
for child in snapshot.children {
let childSnap = child as! DataSnapshot
let uid = childSnap.childSnapshot(forPath: "uid").value as! String
let post = childSnap.childSnapshot(forPath: "post").value as! String
let timestamp = childSnap.childSnapshot(forPath: "timestamp").value as! Int
let thisUserRef = usersRef.child(uid)
thisUserRef.observeSingleEvent(of: .value, with: { userSnap in
index += 1
//for simplicity, I am grabbing only the user name from the user
// data. You could just as easily create a user object and
// populate it with user data and store that in PostClass
// that would tie a user to a post as in the PostUserClass shown above
let userName = userSnap.childSnapshot(forPath: "Name").value as! String
let aPost = PostClass(aPost: post, aUserName: userName, aTimestamp: timestamp)
self.postArray.append(aPost) //or use self.postUserArray to store
// PostUserClass objects in an array.
if index == lastSnapIndex {
self.sortArrayAndDisplay() //or reload your tableView
}
})
}
})
and then the little function to sort and print to console
func sortArrayAndDisplay() {
self.postArray.sort(by: {$0.timestamp > $1.timestamp})
for post in postArray {
print(post.user_name, post.post, post.timestamp)
}
}
Note that Firebase is asynchronous so before sorting/printing we need to know we are done loading in all of the data. This is handled via the lastSnapIndex and index. The index is only incremented once each user is loaded and when all of the posts and users have been loaded we then sort and print as the data is complete.
This example avoids messy callbacks and completion handlers which may be contributing to the issue in the question - this piece of code is suspect and probably should be avoided due to the asynchronous nature of Firebase; the sort function is going to be called well before all of the users are loaded.
UserApi.shared.observeUserToPost(uid: userUid) { (user) in
self.postUser.append(user)
}
self.postUser.sort(by: {$0.postDate! > $1.postDate!})
*please add error checking.

Why .childAdded is not called when new data is added? Firebase

I am trying to read data from media when data is updated on /media node, but .observe(.childAdded is not called.
For example, I update data at /media/-LKN1j_FLQuOvnhEFfao/caption , but I never receive the event in observeNewMedia .
I can read the data with no problem the first time when ViewDidLoad completes.
The first step is to download the user data, second is to get the locality from currentUser and the last step is to attach a listener .childAdded on media.
I suspect that the event is not triggered because fetchMedia is called inside DDatabaseRReference.users(uid: uid).reference().observe(.value
media
-LKNRdP4ZsE3YrgaLB30
caption: "santa"
mediaUID: "-LKNRdP4ZsE3YrgaLB30"
locality: "barking"
users
Q6Dm3IMLNLgBH3ny3rv2CMYf47p1
media
-LKNReJCxgwtGRU6iJmV: "-LKNRdP4ZsE3YrgaLB30"
email: "john#gmail.com"
locality: "barking"
//enables the programmer to create references to different childs in Firebase
enum DDatabaseRReference {
case root
case users(uid:String)
case media //to store photos
func reference() -> DatabaseReference {
return rootRef.child(path)
}
//return root reference to our database
private var rootRef: DatabaseReference {
return Database.database().reference()
}
private var path: String {
switch self { //self is the enum DDatabaseReference
case .root:
return ""
case .users(let uid):
return "users/\(uid)"
case .media:
return "media"
}
}
}//end of enum DatabaseReference
class NewsfeedTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
//observe ~/users/uid
DDatabaseRReference.users(uid: uid).reference().observe(.value, with: { (snapshot) in
DispatchQueue.main.async {
if let userDict = snapshot.value as? [String : Any] {
self.currentUser = UserModel(dictionary: userDict)
self.fetchMedia()
self.tableView.reloadData()
}
}
})
}
func fetchMedia() {
Media.observeNewMedia((currentUser?.locality)!) { (newMedia) in
//check if newly downloaded media is already in media array
if !self.media.contains(newMedia) {
self.media.insert(newMedia, at: 0)
self.tableView.reloadData()
}else {
//remove old media and add the newly updated one
guard let index = self.media.index(of: newMedia) else {return}
self.media.remove(at: index)
self.media.insert(newMedia, at: 0)
self.tableView.reloadData()
}
}
}
}//end of NewsfeedTableViewController
class Media {
class func observeNewMedia(_ userLocality: String, _ completion: #escaping (Media) -> Void) {
DDatabaseRReference.media.reference().queryOrdered(byChild: "locality").queryEqual(toValue: userLocality).observe(.childAdded, with: { snapshot in
guard snapshot.exists() else {
print("no snap ")
return}
print("snap is \(snapshot)")
let media = Media(dictionary: snapshot.value as! [String : Any])
completion(media)
})
}
} //end of class Media
Let's first update the structure so make it more queryable
assume a users node
users
-Q6Dm3IMLNLgBH3ny3rv2CMYf47p1 //this is each users uid
email: "john#gmail.com"
locality: "barking"
and a media node that contains media for all users
media
-abcdefg12345 //node created with childByAutoId
caption: "santa"
for_uid: -Q6Dm3IMLNLgBH3ny3rv2CMYf47p1 //matches the uid in the /users node
Then our main viewController which contains a reference to Firebase and logs the user in
class ViewController: UIViewController {
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
self.ref = Database.database().reference()
//log user in which will populate the Auth.auth.currentUser variable
}
.
.
.
We need an object to store the media in and then an array to hold those objects
class MediaClass {
var key = ""
var caption = ""
init(k: String, c: String) {
self.key = k
self.caption = c
}
}
var mediaArray = [MediaClass]()
then set up the observers which will add, update or remove from the array when media for this user is added, changed or removed.
let thisUid = Auth.auth().currentUser?.uid
let mediaRef = self.ref.child("media")
let queryRef = mediaRef.queryOrdered(byChild: "for_uid").queryEqual(toValue: thisUid)
queryRef.observe(.childAdded, with: { snapshot in
let dict = snapshot.value as! [String: Any]
let key = snapshot.key
let caption = dict["caption"] as! String
let m = MediaClass.init(k: key, c: caption)
self.mediaArray.append(m)
self.tableView.reloadData()
})
queryRef.observe(.childChanged, with: { snapshot in
let dict = snapshot.value as! [String: Any]
let key = snapshot.key
let caption = dict["caption"] as! String
let index = self.mediaArray.index { $0.key == key } //locate this object in the array
self.mediaArray[index!].caption = caption //and update it's caption
self.tableView.reloadData()
})
//leaving this an an exercise
queryRef.observe(.childRemoved....
Note we added .childAdded, .childChanged and .childRemoved events to the media node via a query so the only events the app will receive are the ones that pertain to this user.
Also note there's no error checking so that needs to be added.

Problems Retrieving Data from Firebase in Swift 3

I'm trying to retrieve data from firebase, store it in an array which will then be used as a reference for another firebase query. Unfortunately, I can't seem to get it to work properly. The print function at the bottom is always empty. I have tried to call each successive function after the for loops, but I'm probably doing it wrong.
I have 2 branches in firebase that looks a bit like this:
-id
|
Group1
|
ID01: Created on 01.01.16
-idDetails
|
ID01
//name: name01
//description: description01
This is my code:
var array = [String]()
var id = [String]()
var items = [Item]() //Item Class Array
override func viewDidLoad() {
super.viewDidLoad()
getIdFromFirebase()
}
//first task
func getIdFromFirebase(){
for index in array //index is being used as part of path query
{
FireDbase_Main.child("Group1").child("id").child(index).observe(FIRDataEventType.childAdded, with: { (snapshot) in
self.id = (snapshot.key)
id.append(id)
})
getDataForItems()
}
}
//second task
func getDataForItems() {
let newItem = Item()
for index in id //i is being used as part of path query
{
FireDbase_Main.child("idDetails").child(index).observeSingleEvent(of: .value, with: { (snapshot) in
newItem.itemID = snapshot.key
newItem.name = (snapshot.value as! NSDictionary)["name"] as! String
newItem.desc = (snapshot.value as! NSDictionary)["description"] as! String
self.items.append(newItem)
}
printItemsArray(
}
//3rd Task: For now I have put a print here just as a placeholder task.
func printItemsArray(){
print(items.map {$0.itemID})
print(items.map {$0.name})
print(items.map {$0.desc})
}