Reputation: 3961
High level: In Cloud Firestore, I have two collections. fl_content
and fl_files
. Within fl_content
, I am trying to access fl_files
.
Detailed: In fl_content, each document has a field called imageUpload
. This is an array of Firebase Document References. (a path to fl_files
that I need to access.)
Here's my query for fl_content, in which I am accessing imageUpload reference:
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload")
print("PROPERTY \(property!)")
}
}
This prints the following to the console:
PROPERTY Optional(<__NSArrayM 0x60000281d530>(
<FIRDocumentReference: 0x600002826220>
)
)
With this array of Document References, I need to get to fl_files.
This is the part I am having trouble with.
Attempts:
Within the if let statement, I tried accessing fl_files by casting property as a DocumentReference.
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload") as? DocumentReference
print("PROPERTY \(property!)")
let test = Firestore.firestore().collection("fl_files").document(property)
}
}
Cannot convert value of type 'DocumentReference?' to expected argument type 'String'
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload") as! DocumentReference
let test = Firestore.firestore().collection("fl_files").document(property[0].documentID)
print("TEST \(test)")
}
}
Value of type 'DocumentReference' has no subscripts
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("imageUpload") as! DocumentReference
let test = Firestore.firestore().collection("fl_files").document(property.documentID)
print("TEST \(test)")
}
}
Could not cast value of type '__NSArrayM' (0x7fff87c50980) to 'FIRDocumentReference' (0x10f6d87a8). 2020-02-05 12:55:09.225374-0500 Database 1[87636:7766359] Could not cast value of type '__NSArrayM' (0x7fff87c50980) to 'FIRDocumentReference' (0x10f6d87a8).
Getting closer!
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
docRef.getDocument(completion: { document, error in
if let err = error {
print(err.localizedDescription)
return
}
let imageUpload = document?["imageUpload"] as? NSArray ?? [""]
print("First Object \(imageUpload.firstObject!)")
})
This prints: First Object <FIRDocumentReference: 0x600001a4f0c0>
Here are two screenshots to help illustrate what the Firestore database looks like..
Ultimately, I need to get to the file
field within fl_files. How do I access this from the imageUpload DocumentReference
?
Upvotes: 3
Views: 3570
Reputation: 3961
Finally got it, thanks to the help of @Jay and @Emil Gi.
The "A-HA" moment came from Emil Gi's comment: All I can say is that if you successfully get element of DocumentReference type, then it must have an id property, which you can extract and query collection by document id.
let imageUploadReference = item.imageUpload.first as? DocumentReference
let docRef = Firestore.firestore().collection("fl_files").document(imageUploadReference!.documentID)
docRef.getDocument(completion: { document, error in
if let err = error {
print(err.localizedDescription)
return
}
let fileNameField = document?.get("file") as! String
print("File name from fl_files \(fileNameField)")
})
Once I finally had access to the corresponding "file", it was very simple to download the full URL of the image to the imageView.
I appreciate all of your help!!!
Upvotes: 3
Reputation: 35658
Here's some sample code that shows how to read and print out any of the fields values and also how to read the imageUpload field (an array) and print out the first element's value.
I've included both ways to read data from a document because I believe it answers both parts of the question: how to get the array field imageUpload and then access the first element within that array and how to get the value of the file field within fl_files.
Assuming this points to the correct document:
let docRef = Firestore.firestore().collection("fl_content").document(item.id)
this code will read two fields from the document being pointed to: imageUpload (an array) and fl_id (a String)
docRef.getDocument(completion: { document, error in
if let err = error {
print(err.localizedDescription)
return
}
print( document!.data() ) //for testing to see if we are getting the fields
//how to read an array and access it's elements
let imageUploadArray = document?["imageUpload"] as? Array ?? [""]
if let url = imageUploadArray.first {
print(url)
}
//this is also valid for reading the imageUpload field array
//let anArray = document?.get("imageUpload") as? Array ?? [""]
//if let url = anArray {
// print(url)
//}
//how to read a single field, like file within fl_files
let someId = document?.get("fl_id") as! String //example of reading a field
print(someId)
})
Edit:
It appears the objects stored in the imageUpload array are references not strings. As an example of how to access them... here's the code
let refArray = document?.get("imageUpload") as? [DocumentReference] ?? []
for docRef in refArray {
print(docRef.path) //prints the path to the document at this ref
}
Upvotes: 2