'Repeat execution of program Swift
I’ve a program that takes some system information from mac, like User List and Last Sessions.
I have a configuration plist with some values, I serialized this plist and created an AssessmentConfiguration object with this data My Plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>OutputFile</key>
<string>/tmp/empress_assessment_output.txt</string>
<key>ErrorFile</key>
<string>/tmp/empress_assessment_error.txt</string>
<key>RunConfiguration</key>
<dict>
<key>RunInterval</key>
<integer>30</integer>
<key>Iterations</key>
<integer>3</integer>
</dict>
</dict>
</plist>
My function using the plist values to init AssessmentConfiguration object
private func parseConfiguration(dictionary: Dictionary<String, Any>) throws -> AssessmentConfiguration {
guard let outputFile = dictionary["OutputFile"] as? String else {
throw ValidateError.empty
}
guard let errorFile = dictionary["ErrorFile"] as? String else {
throw ValidateError.empty
}
guard let runConfiguration = dictionary["RunConfiguration"] as? Dictionary <String, Any> else {
throw ValidateError.empty
}
guard let runInterval = runConfiguration["RunInterval"] as? TimeInterval else {
throw ValidateError.empty
}
guard let iterations = runConfiguration["Iterations"] as? Int else {
throw ValidateError.empty
}
return AssessmentConfiguration(
outputFile: URL(fileURLWithPath: outputFile),
errorFile: URL(fileURLWithPath: errorFile),
runConfiguration: AssessmentConfiguration.RunConfiguration(runInterval: runInterval, iterations: iterations)
)
}
func getConfiguration() throws -> AssessmentConfiguration {
let path = try getConfigurationFilePath()
let myDict = try getDictionary(filePath: path)
let configuration = try parseConfiguration(dictionary: myDict)
return configuration
}
The program will executed several times. The RunConfiguration object will configure how this will happen. ⁃RunInterval represents the amount of time between the executions of the commands set. ⁃Iterations represents the number of iterations that the commands set will be executed. ⁃ E.g.: If RunInterval == 30 and Iterations == 3, the commands set will run at start, then again after 30 seconds, and again util all the commands have been executed 3 times and terminate with exit code 0.
What's the best way to do this with the return of getConfiguration()?
Solution 1:[1]
I see you're going down the route of implementing this scheduling behaviour by yourself. It basically ultimately boils down to using a for loop and a Timer, but I wouldn't suggest this, because:
- This "supervisor" code will have to be constantly running, wasting system resources.
- It's more work, because you have to build it yourself.
Instead, I recommend you use the existing facilities, which for macOS, is to use launchd to launch a launch agent or daemon. All you have to define is a job (a plist file), which configures what you want to run, and in response to what events (it can be time, but there's also support for running in response to a file showing up in a folder, a USB device being plugged in, etc.). From there, launchd will take care of starting your program at the right time.
See Apple's Daemons and Services Programming Guide for the full details.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexander |
