A singleton pattern guarantees that only one instance of a class is initialized and available from different points of an app.
Some examples are already available in Apple’s frameworks:
// Shared URL Sessionlet sharedURLSession = URLSession.shared// Default File Managerlet defaultFileManager = FileManager.default// Standard User Defaultslet standardUserDefaults = UserDefaults.standard
How to define a Singleton
Often a static constant is used to adopt the Singleton pattern. To do that the reference to the shared instance is stored inside a static constant. In addition, the initializer is private and therefore hidden:
By making the initializer method private, it’s guaranteed that no other instance of this class can be created. It would also be possible to move the static constant outside the class, but then the initializer might be accessible and that’s not what we want.
As already discussed in Background task in iOS action extension, it sometimes becomes necessary to perform time consuming tasks in the background. This is really important, if such a task would block the user interaction. Especially for action and share extensions, this might lead to some waiting time before a task completes and the extension disappears. After several attempts to realise a working solution, the following code help to extends an App’s background execution time.
funcperformTask(){// Perform the task on a background queue. DispatchQueue.global().async {// Request the task assertion and save the ID.self.backgroundTaskID = UIApplication.shared.beginBackgroundTask (withName: "Perform Long Task") {// End the task if time expires. UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)self.backgroundTaskID = UIBackgroundTaskInvalid }// Run task synchronously.self.performLongTask()// End the task assertion. UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)self.backgroundTaskID = UIBackgroundTaskInvalid }}
Extending the background execution time in an extension
funcperformTask(){// Perform the task in background.let processinfo = ProcessInfo() processinfo.performExpiringActivity(withReason: "Long task") { (expired) inif (!expired) {// Run task synchronously.self.performLongTask() }else {// Cancel task.self.cancelLongTask() } }}
As mentioned in the documentation, the ProcessInfo code block is called a second time if the system need to suspend the process:
If your block is still executing and the system need to suspend the process, the system executes your block a second time with the expired parameter set to true. Your block must be prepared to handle this case. When the expired parameter is true, stop any in-progress tasks as quickly as possible.
Important: it’s important that the tasks are executed synchronously. When the end of the block is reached, the thread will terminate and end the execution of your task. If your tasks are asynchron, you can use a loop like shown below to wait inside the background thread until the asynchron task is finished:
In Swift lassen sich Audioaufnahmen sehr komfortabel und einfach über die Klasse AVAudioRecorder realisieren. Beim Initialisieren der Klasse lassen sich bereits Codec, Samplerate, Anzahl der Kanäle und einige andere wichtige Einstellungen festlegen. Hier ein einfaches Code-Beispiel: