Published on August 2, 2022

Get a notification when your Swift code is done executing

Get a notification when your Swift code is done executing

These days we can use Swift for almost anything, be it a simple function, a complex algorithm, or anything that comes to mind. Usually, what we want Swift to do happens very quickly, and we don't even have to wait for it to finish.

Long-running Swift tasks are a problem

However, sometimes you are writing a Swift code, process, or job that may take a long time to run. For example, you may decide to crawl a website or run a long-running parsing algorithm.

I'm sure almost every developer has, at some point, written a Swift code that takes a long time to run. They then had to continuously check the task, wait for it to finish running, and ensure the Swift code was working as expected. As you can see, this gets tedious very quickly and wastes time and effort.

Push notifications fix this problem

One common way to solve this is to track the status of our Swift code and send a push notification when the code is done running or when it fails. By doing so, we can leave and forget about the long-running Swift code and focus on the important things we need to do, and by the time something happens, we will instantly get a push notification to let us know.

To do so, we can use LogSnag to track the status of our Swift code. LogSnag is a simple, easy-to-use, robust event tracking and notification system that lets you track your Swift code and send push notifications when something happens.

Let's walk you through setting up and using LogSnag to track the status of our Swift code.


Setting up LogSnag

  1. Sign up for a free LogSnag account.
  2. Create your first project from the dashboard.
  3. Head to settings and copy your API token.

Swift code snippets

Now that we have our account and project setup, we can copy the following code snippet, update the values with your information, and paste it into your code.

Using Swift with URLSession
let parameters = "{\"project\":\"my-project\",\"channel\":\"crawler\",\"event\":\"Crawling is complete\",\"description\":\"Successfully crawled 1,230 pages.\",\"icon\":\"🔥\",\"notify\":true}"
let postData = parameters.data(using: .utf8)

var request = URLRequest(url: URL(string: "https://api.logsnag.com/v1/log")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer YOUR_API_TOKEN", forHTTPHeaderField: "Authorization")

request.httpMethod = "POST"
request.httpBody = postData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
return
}
print(String(data: data, encoding: .utf8)!)
}

task.resume()

Swift integration details

LogSnag provides several other features that can be used to simplify your job as a developer. For example, suppose you're working with a team. In that case, you can also add the rest of your team to your LogSnag project and allow them to see the status of your Swift code and receive push notifications when something important happens.

LogSang also keeps track of your previous events, so you can see what has happened in the past and search through previous events. We found this to be very powerful when we are working on larger projects and need to see what has happened in the past.

In addition, LogSnag allows you to create simple dashboards, charts, and graphs that give you more insight into your code and the events you track.

Other use-cases for LogSnag

  1. Monitor your CI/CD build status for your Swift application
  2. Monitor your CPU usage in your Swift application
  3. Monitor when database goes down in your Swift application
  4. Monitor high disk usage in your Swift application
  5. Monitor when a user changes their email address in your Swift application
  6. Monitor failed logins in your Swift application
  7. Monitor failed payments for your Swift application
  8. Monitor memory usage in your Swift application
  9. Monitor MySQL downtime in your Swift application
  10. Monitor when a new feature is used in your Swift application
  11. Monitor your Postgres downtime in your Swift application
  12. Monitor Redis downtime in your Swift application
  13. Monitor suspicious activity in your Swift application
  14. Monitor when a user exceeds the usage limit for your Swift service
  15. Monitor when a user is being rate limited in your Swift application
  16. Send push notifications to your phone or desktop using Swift
  17. Track canceled subscriptions in your Swift application
  18. Track your Swift cron jobs
  19. Track when a file is uploaded to your Swift application
  20. Track when a form is submitted to your Swift application
  21. Track payment events via Swift
  22. Track user sign in events in Swift
  23. Track user signup events via Swift
  24. Track waitlist signup events via Swift
View all common use-cases with Swift