Nested explicit wait does not work in Appium

For example, I’m sending a message. While it’s being sent to the recipient it has the resource-id “pending_indicator”. When it is delivered, it has the resource-id “delivered_indicator”.

So, I’m checking if the message is delivered using the following code:

List<MobileElement> messages = driver.findElements (By.id ("body_bubble")); // Get all messages in chat
MobileElement lastMessage = messages.get (messages.size () - 1);            // Get the last message

//Xpath query to check if the message has been delivered
String query = "//*[contains(@resource-id, 'delivered_indicator')]";

WebDriverWait wait = new WebDriverWait (driver, 10);
wait.until (ExpectedConditions.visibilityOfNestedElementsLocatedBy (lastMessage, By.xpath(query)));

But it does not work. I checked that the message has been delivered within 10 seconds, but the wait does not return anything. When I checked in IntelliJ evaluation mode, it showed that the lastMessage still has the “pending_indicator” resource-id in the “footer_delivery_status” container. Why is it not updated?

The lastMessage variable (message body) stays the same in app sources. It does not change itself, so it should not affect the result.

I also tried to use this wait, but it did not help:

wait.until(ExpectedConditions.visibilityOf (lastMessage.findElement (By.xpath(query))));

However when I use this Xpath, it works properly:

"(//*[contains(@resource-id,'body_bubble')])[last()]//*[contains(@resource-id, 'delivered_indicator')]"

So why it does not work with the nested explicit wait?