# Keep getting EXC\_BAD\_ACCESS when sending message

**URL:** <https://forum.social.plus/t/keep-getting-exc-bad-access-when-sending-message/1174>\
**Category:** iOS\
**Created:** [March 5, 2024, 4:54pm UTC](https://forum.social.plus/t/keep-getting-exc-bad-access-when-sending-message/1174 "2024-03-05T16:54:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![parthib.weavers](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@parthib.weavers](https://forum.social.plus/u/parthib.weavers)\
**Post date:** [March 5, 2024, 4:54pm UTC](https://forum.social.plus/t/keep-getting-exc-bad-access-when-sending-message/1174/1 "2024-03-05T16:54:37Z")

</div>

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:

```auto
@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:

```auto
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:

 ![Screenshot 2024-03-05 at 10.13.42 PM](https://europe1.discourse-cdn.com/flex013/uploads/amity/original/1X/cecfcda323c8a8ae59a852e950fc6e2273f93dc7.png)

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.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [March 6, 2024, 4:32am UTC](https://forum.social.plus/t/keep-getting-exc-bad-access-when-sending-message/1174/2 "2024-03-06T04:32:23Z")

</div>

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