I think it’s always a matter akin to what God did in the Garden of Eden when he called out asking Adam where he was. It wasn’t that he didn’t know, but he called out for the sake of Adam. Same thing with a parent who catches a child in the act of doing something wrong, but still says, “What are you doing?” It’s conversation for the benefit of the human beings involved. Just as Jesus said when raising Lazarus that He knew His Father heard them, but asked for the benefit of the people listening, and also how the Voice from Heaven that sounded like thunder to some, He said the Voice came for the benefit of some there.
“...asked for the benefit of the people listening...”
I think Jesus talking about the children's food and not giving it to the dogs (and then relenting) was for His disciple's benefit as well. I'm sure they remembered this event when they were prompted by the Holy Spirit to advance the Gospel to the Gentiles.
Another example is when the woman touched Jesus's garment to be healed of her illness, and in turn, Jesus asked who touched Him.
He knew people would scoff at such a question because He was in the midst of a crowd.
But Jesus also knew who touched Him.
He wanted her to speak up and witness in front of everyone about her faith in Him and how it healed her.