use mainContext instead of ctx. replace ctx in other places with context