Keep getting EXC_BAD_ACCESS when sending message

I’ve implemented the AmitySDK framework in my app. Sending messages was working without a hitch, but then it randomly started crashing due to Realm while sending message.

What I’ve done so far:

  • Clean-built the project
  • Reinstalled the app
  • Restarted simulator
  • Installed on other simulators

I’ve done all of the above, and yet the app still crashes while sending a message.

This is the code I wrote for logging in:

@discardableResult
    func login() async -> Bool {
        let userID = "user_testing"
        let displayName = "USER TESTING"
        var success = false
        
        do {
            guard let _ = self.client else { return false }
            
            try await self.client!.login(userId: userID, displayName: displayName, authToken: nil, sessionHandler: MySessionHandler())
            
            DispatchQueue.main.async {
                self.userNotificationToken = self.client?.user?.observeOnce({ user, error in
                    self.user = user.snapshot
                    
                    if user.snapshot?.avatarFileId == nil {
                        //Updating profile pictuer of user
                    }
                    
                    if usernames.contains(userID) && user.snapshot?.metadata?["userType"] == nil {
                        //Update metadata of user
                    }
                })
            }
            
            success = true
            return success
            
        } catch {
            success = false
            return success
        }
    }

This one for sending message:

func sendTextMessage(_ message: String, mentionRanges: [AmityMention], in subchannel: String) async {
        var tags: [String] = []
        
        if let userInfo = self.user?.metadata?["userType"] as? String {
            //Adding particular tags here
        } else {
            //Adding some other tags
        }
        
        let mentionMetadata = AmityMentionMapper.metadata(from: mentionRanges)
        
        var mentionees: AmityMentioneesBuilder?
        
        if !mentionRanges.isEmpty {
            mentionees = AmityMentioneesBuilder()
            mentionees?.mentionUsers(userIds: mentionRanges.compactMap({ $0.userId }))
        }
        
        var mergedMetadata = mentionMetadata
        
        if let userMetadata = self.user?.metadata {
            mergedMetadata = mergedMetadata.merging(userMetadata, uniquingKeysWith: { $1 })
        }
        
        let textMessageCreationOptions = AmityTextMessageCreateOptions(subChannelId: subchannel, text: message, tags: tags, metadata: mergedMetadata, mentioneesBuilder: mentionees)
        
        do {
            let message = try await self.messageRepository?.createTextMessage(options: textMessageCreationOptions)
        } catch {
            print("Unable to send text message. Error: \(error.localizedDescription)")
        }
    }

If it helps, this is where it crashes:
Realm → Query → func build(_ node: QueryNode, prefix: String? = nil, isNewNode: Bool = false)

This function calls recursively and crashes here:

In the Debug Navigator, I tried to go back step by step in the stacktace to investigate which field was causing this. The issue started from appendKeyPath(_:options:) method in the Query struct.

Apparently whenever I send a message, the SDK does something with Realm and always messes up with messageFeedId field from _ASCMessageRealmModel.

Please let me know where I went wrong.

@parthib.weavers Please send the full crash log to our support team at support.asc@amity.co. It will significantly aid in our investigation of the issue.