RxSwiftCommunity / RxFirebase

RxSwift extensions for Firebase
MIT License
224 stars 66 forks source link

Query listener does not work when 'Where' clause is added #36

Closed WestFlow127 closed 3 years ago

WestFlow127 commented 3 years ago

I have a query that provides 'feed' data to a collection view using RxSwift and RxFirebase. This feed data now has a 'Privacy' field (possible values: "Public", "Sub", and "Private") and I need to filter out the 'Private' entities. However when I add a 'Where' clause to do this, the listener no longer adds newly posted entities from this collection. I do not know why this is happening, any help would be appreciated.

Here is the current query code:

func fetchGlobalFeed(limit: Int, startAfter: Timestamp?, listens: Bool) -> Observable<[Entity.Fragment]> {
     var query = db.collection(k.mCollection)
             .limit(to: limit)
            .order(by: "PublishedAt", descending: true)

    if let timestamp = startAfter {
        query = query.start(after: [timestamp])
    }

    let observable = listens ? query.rx.listen() : query.rx.getDocuments()
    return observable
        .catchErrorJustComplete()
        .map(FirestoreHelpers.dataFromQuerySnapshot)
        .map { data in
            data.compactMap {
                try? FirestoreDecoder()
                    .decode(Entity.Fragment.self, from: $0)
            }
        }
}`

And changing the query to: var query = db.collection( k.mCollection ) .limit( to: limit ) .WhereField( "Privacy", in: ["Public", "Sub"] ) // this causes issue with listener .order( by: "PublishedAt", descending: true )

Maybe an explanation of how listeners work and any suggestions would be appreciated. Thanks.

RxFirebase version: 0.3.8

engali94 commented 3 years ago

Please can you debug your observable using the debug operator to check whether your sequence gets disposed due to an error or not?

The other possible reasons are:

  1. No matching documents for your query.
  2. you may need to create a custom index for your query (which will throw an error and terminate the sequence). The error contains the guiding info you can follow to create the index.
WestFlow127 commented 3 years ago

Here is the debug output:

2020-09-10 11:37:03.101: MagmaFirestoreProvider.swift:165 (fetchGlobalFeed(limit:startAfter:listens:)) -> subscribed 2020-09-10 11:37:03.324: MagmaFirestoreProvider.swift:165 (fetchGlobalFeed(limit:startAfter:listens:)) -> Event next([Entity.Fragment(ref: <FIRDocumentReference: 0x60000109ef40>, publisher: Optional(UserEntity.Fragment(ref: <FIRDocumentReference: 0x60000109ee80>, username: "Art_Collective", name: nil ......... privacy: Optional(magma.MagmaMagPrivacy.Public))]) 2020-09-10 11:37:03.458: MagmaFirestoreProvider.swift:165 (fetchGlobalFeed(limit:startAfter:listens:)) -> Event completed 2020-09-10 11:37:03.458: MagmaFirestoreProvider.swift:165 (fetchGlobalFeed(limit:startAfter:listens:)) -> isDisposed

When a new item is posted to the feed with a privacy of "Sub", the item does not appear and there is zero debug output. I have to refresh the collection view to get the above output and the new item in the feed. I assume that's because the observable is being disposed?

WestFlow127 commented 3 years ago

Creating a composite index for the query worked! Thank you for the suggestion.