Solution for Swift error with reason: ‘Only run on the main thread!’