Divine Providence
Sometimes God chooses to intervene for us and sometimes He chooses not to. But in both instances, He is acting on our behalf, for our good and His glory.
God chose to withhold His hand of protection towards David as a punishment for his sin. But He did not withdraw His protection completely or forever. He still had favor on David and took care of him until everything was restored back to him.