The Gemara names King David as an example of a righteous individual who was, nevertheless, concerned lest he lose his portion in the World-to-Come. The Gemara explains that although King David recognized his own status, he was concerned shema yigrom ha-het – lest a transgression that he might commit in the future would cause him to lose what he rightly deserved.
To support the idea of shema yigrom ha-het the Gemara offers examples of situations where transgressions are understood to have changed the course of history. One example is the return of the Jewish people to the Land of Israel at the beginning of the Second Temple period, which took place naturally without any obvious miracles.
The Gemara relates that when Moshe Rabbeinu sang the Song of the Sea he referred prophetically to the entrance of the Children of Israel into the land with Yehoshua and then again with Ezra (see Shemot 15:16-17). Based on the juxtaposition of these two entries in this single verse, the Sages said: “Israel was worthy of having a miracle performed on its behalf in the time of Ezra the scribe, just as one was performed on their behalf in the time of Yehoshua bin Nun. However, transgression caused the absence of a miracle.”
In both the Rambam’s Mishneh Torah, (Hilkhot Yesodei HaTorah, Chapter 10), and in his introduction to his commentary on the Mishna, he writes that promises made by God by means of a prophet will never be retracted and that no sin can affect those promises. This appears to contradict the statement that transgression caused the undoing of Moses’ promise to the Jewish people that entry into Eretz Yisrael during Ezra’s time would be miraculous. The Anaf Yosef explains this discrepancy based on the fact that the Rambam wrote this in the context of explaining how to ascertain who is a true prophet. Positive prophecies made by a true prophet are always fulfilled lest questions be raised about the prophet’s legitimacy. Since there is no question that Moses was a true prophet, even if one of his prophecies was not realized due to the people’s transgression, no doubts would be raised with regard to his status as a prophet.