0
votes

Update

For anybody who also is having similar problem in the future. I am not sure why the ObjectiveC-SDK with documentWithPath and documentWithCollection is not working but what i have found is if we directly give the path it can work.

So instead of asking

self.db collectionWithPath:@"stage_tickets"]documentWithPath:bookingReference]collectionWithPath:@"tickets"]getDocumentsWithCompletion

and getting nothing in return you can directly write the path

self.db collectionWithPath:@"stage_tickets/{bookingReference}/tickets"]getDocumentsWithCompletion


I have a Cloud FireStore database with a structure like this.

Cloud Firestore Database

The Structure is basically stage_tickets(Collection)->Numbers(Documents)->Tickets(Collection)->Numbers(Documents)

The really Strange thing is if i want to get all the numbers from the first level i can easily get them via

[[self.db collectionWithPath:@"stage_tickets"]
          getDocumentsWithCompletion:^(FIRQuerySnapshot *snapshot, NSError *error) {
                      if (error != nil) {
                        NSLog(@"Error getting documents: %@", error);
                      } else {
                        for (FIRDocumentSnapshot *document in snapshot.documents) {
                          NSLog(@"%@ => %@", document.documentID, document.data);
                        }
                      }
}];

But if i try to reach the second level of numbers via

[[[[self.db collectionWithPath:@"stage_tickets"]documentWithPath:bookingReference]collectionWithPath:@"tickets"]getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot, NSError * _Nullable error) {
                    if(error!=nil){
                        NSLog(@"error in getting QR Code");
                    }else{
                        for (FIRDocumentSnapshot *document in snapshot.documents) {
                            NSLog(@"%@ => %@", document.documentID, document.data);
                        }
                    }
}];

All i get is empty documents in Snapshot. Can anybody suggest what is wrong i am doing here, maybe some behaviour of fireStore that i am unaware of and how can i fix this.

1
you want to get stage_tickets(Collection)->Numbers(Documents)->Tickets(Collection)>Numbers(Documents) this documents from first stage_tickets(Collection)->Numbers(Documents) or all Tickets(Collection) documents??Jignesh Mayani
I basically want to get all ticket(Documents/the last column) from the tickets collection in second column.dev_ios999

1 Answers

2
votes

You can get all ticket's document data using CollectionGroup like as below

self.db.collectionGroup("tickets").getDocuments { (snapshot, error) in
        
        if let err = error
        {
            print(err)
        }
        else
        {
            if let snapshot = snapshot,snapshot.documents.count > 0
            {
                for document in snapshot.documents
                {
                    print(document.data())
                }
            }
            
        }
    }

You can get all documents for particular collection as below

self.db.collection("stage_tickets").document("00005055").collection("tickets").getDocuments { (snapshot, error) in
        
        if let err = error
        {
            print(err)
        }
        else
        {
            if let snapshot = snapshot,snapshot.documents.count > 0
            {
                for document in snapshot.documents
                {
                    print(document.data())
                }
            }
            
        }
    }