How to use a timer in background task in swift framework?

Hello! I'm developing an app which related to sensor. I want to get sensor data continuously in foreground and even when I press home button, it should works in background.


I use a timer to fetch data and put it in a runloop. How can I use BackgroundTasks to make it work? If it works, is there a time limit after I run my app in background, after this time maybe my app will be killed by system?


Thanks