Skip to content

FIRESTORE INTERNAL ASSERTION FAILED: QueryComparator needs to have a key ordering. (expected has_key_ordering) #7672

Closed
@isharing0726

Description

@isharing0726

[REQUIRED] Step 1: Describe your environment

  • Xcode version: 12.4
  • Firebase SDK version: 6.32.2
  • Installation method: CocoaPods
  • Firebase Component: Firestore

[REQUIRED] Step 2: Describe the problem

Fatal Exception: NSInternalInconsistencyException

FIRESTORE INTERNAL ASSERTION FAILED: QueryComparator needs to have a key ordering. (expected has_key_ordering)
Fatal Exception: NSInternalInconsistencyException

0  CoreFoundation                 0x186369654 __exceptionPreprocess
1  libobjc.A.dylib                0x18608bbcc objc_exception_throw
2  CoreFoundation                 0x18626c6ec +[_CFXNotificationTokenRegistration keyCallbacks]
3  Foundation                     0x186700584 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:]
4  iSharing                       0x100900180 firebase::firestore::util::ObjcThrowHandler(firebase::firestore::util::ExceptionType, char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 59 (exception_apple.mm:59)
5  iSharing                       0x1008ffe34 firebase::firestore::util::Throw(firebase::firestore::util::ExceptionType, char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 90 (exception.cc:90)
6  iSharing                       0x10094e0d4 firebase::firestore::util::internal::FailAssertion(char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) + 42 (hard_assert.cc:42)
7  iSharing                       0x10094e174 firebase::firestore::util::internal::FailAssertion(char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) + 49 (hard_assert.cc:49)
8  iSharing                       0x10098c35c firebase::firestore::core::Query::Comparator() const + 295 (query.cc:295)
9  iSharing                       0x1009c9e4c firebase::firestore::core::View::View(firebase::firestore::core::Query, firebase::firestore::immutable::SortedSet<firebase::firestore::model::DocumentKey, firebase::firestore::util::Comparator<firebase::firestore::model::DocumentKey> >) + 89 (view.cc:89)
10 iSharing                       0x1009b4008 firebase::firestore::core::SyncEngine::InitializeViewAndComputeSnapshot(firebase::firestore::core::Query const&, int) + 98 (sorted_map.h:98)
11 iSharing                       0x1009b377c firebase::firestore::core::SyncEngine::Listen(firebase::firestore::core::Query) + 435 (vector:435)
12 iSharing                       0x1008fce04 firebase::firestore::core::EventManager::AddQueryListener(std::__1::shared_ptr<firebase::firestore::core::QueryListener>) + 60 (event_manager.cc:60)
13 iSharing                       0x10092562c std::__1::__function::__func<firebase::firestore::core::FirestoreClient::ListenToQuery(firebase::firestore::core::Query, firebase::firestore::core::ListenOptions, std::__1::shared_ptr<firebase::firestore::core::EventListener<firebase::firestore::core::ViewSnapshot> >&&)::$_10, std::__1::allocator<firebase::firestore::core::FirestoreClient::ListenToQuery(firebase::firestore::core::Query, firebase::firestore::core::ListenOptions, std::__1::shared_ptr<firebase::firestore::core::EventListener<firebase::firestore::core::ViewSnapshot> >&&)::$_10>, void ()>::operator()() + 4211 (memory:4211)
14 iSharing                       0x1008e1598 firebase::firestore::util::AsyncQueue::ExecuteBlocking(std::__1::function<void ()> const&) + 957 (atomic:957)
15 iSharing                       0x1009c0330 firebase::firestore::util::Task::ExecuteAndRelease() + 1859 (functional:1859)
16 libdispatch.dylib              0x18602f524 _dispatch_client_callout
17 libdispatch.dylib              0x185fdb8a4 _dispatch_lane_serial_drain$VARIANT$mp
18 libdispatch.dylib              0x185fdc294 _dispatch_lane_invoke$VARIANT$mp
19 libdispatch.dylib              0x185fe578c _dispatch_workloop_worker_thread
20 libsystem_pthread.dylib        0x186080b74 _pthread_wqthread
21 libsystem_pthread.dylib        0x186083740 start_wqthread

Steps to reproduce:

Occurring on lots of customer devices. Don't know how to reproduce it.

Relevant Code:

- (void)getChatMessages:(NSString*)room
                 token:(NSString *)token
                 source:(FIRFirestoreSource)source
            completion:(void(^)(NSError * _Nullable error, NSArray<ChatMessage *> * _Nonnull messageList, NSString * _Nullable nextToken))completion
{
    const NSUInteger FETCH_COUNT = 30;
    FIRCollectionReference *ref = [[[self.db collectionWithPath:SCHEMA_CHAT_MESSAGES]
                                               documentWithPath:room]
                                             collectionWithPath:COLLECTION_MESSAGES];
    
    FIRQuery *query = [ref queryOrderedByField:@"timestamp" descending:YES];
    if (token) {
        NSInteger startTimestamp = [token integerValue];
        query = [query queryStartingAtValues:@[@(startTimestamp)]];
    }
    query = [query queryLimitedTo:FETCH_COUNT];
    
    [query getDocumentsWithSource:source
                       completion:^(FIRQuerySnapshot * _Nullable snapshot,
                                    NSError * _Nullable error) {
        if (snapshot == nil) {
            ELog(@"ClientAPIFS Error fetching document: %@", error);
            return;
        }
        
        NSMutableArray *messageList = [[NSMutableArray alloc] init];
        for (FIRQueryDocumentSnapshot *doc in snapshot.documents) {
            ChatMessage *message = [[ChatMessage alloc] initWithKey:doc.documentID data:doc.data];
            message.room = room;
            [messageList addObject:message];
        }
        
        NSString *nextToken = nil;
        if (snapshot.documents.count >= FETCH_COUNT) {
            FIRQueryDocumentSnapshot *doc = [snapshot.documents lastObject];
            NSNumber *timestamp = doc.data[@"timestamp"];
            nextToken =  [timestamp stringValue];
        }
        
        completion(nil, messageList, nextToken);
    }];
}

- (void)listenChatMessageChangeFor:(sw_id_t)friendId room:(NSString *) room onReceive:(void(^)(ChatMessage *data))callback
{
    
    const NSInteger DELAY_THRESHOLD = 60;
    const NSInteger now = [[NSDate date] timeIntervalSince1970] - DELAY_THRESHOLD;

    FIRCollectionReference *ref = [[[self.db collectionWithPath:SCHEMA_CHAT_MESSAGES]
                                               documentWithPath:room]
                                             collectionWithPath:COLLECTION_MESSAGES];
    
    if (_messageChangedListener){
        [_messageChangedListener remove];
    }
    _messageChangedListener = [ref addSnapshotListener:^(FIRQuerySnapshot * _Nullable snapshot,
                                                         NSError * _Nullable error) {
        
        if (snapshot == nil) {
            ELog(@"ClientAPIFS Error fetching document: %@", error);
            return;
        }
        
        for (FIRDocumentChange *diff in snapshot.documentChanges) {
            if (diff.type == FIRDocumentChangeTypeAdded) {
                ChatMessage *message = [[ChatMessage alloc] initWithKey:diff.document.documentID data:diff.document.data];
                message.room = room;
                
                if (message.timestamp >= now) {
                    self.callbackMessageAdded(message);
                }
            }
            
            if (diff.type == FIRDocumentChangeTypeModified) {
                ChatMessage *message = [[ChatMessage alloc] initWithKey:diff.document.documentID data:diff.document.data];
                message.room = room;
                callback(message);
            }
        }
    }];

}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions