he gained it shortly before his death
i dont think he gained it right before his death, because he was like 80 when he meat obito and im pretty sure he had the rinnegan way before he meat obito. as the guy the threat i too belive he lost to hasimara on purpose so he can get the rinnegan