Thursday, June 13, 2024
HomeiOS Developmentios - Synchronising a MainActor remoted technique throughout init and satisfying swift...

ios – Synchronising a MainActor remoted technique throughout init and satisfying swift 6 concurrency


I’ve a LogManager singleton class that saves strings into an array for logging functions. I would like the primary entry into the log to be some system info so it will get added throughout init

@objc
public ultimate class LogManager: NSObject, @unchecked Sendable {
    
    static let sharedInstance = LogManager()
    personal var _logs: [String] = []
    personal let serialQueue = DispatchQueue(label: "LoggingSerialQueue")
    
    personal override init() {
        tremendous.init()
        
        log(SysInfo.sysInfo())
    }
    
    func log(_ log: String) {
        serialQueue.sync {
            self._logs.append(log)
            print("****LogManager**** (log)")
        }
    }
}

The issue is that the system info must be MainActor remoted because it calls MainActor remoted properties on UIDevice and a number of other others.

personal ultimate class SysInfo {
    
    @MainActor
    static func sysInfo() -> String {
        UIDevice.present.systemName
    }
}

I don’t need to drive my LogManager to be MainActor remoted as it may be referred to as from many various threads.

I do know I can add the log name from init right into a Job like this:

personal override init() {
    tremendous.init()
    
    Job {
        await log(SysInfo.sysInfo())
    }
}

however that truly results in it being the second log within the array and never the primary, once I provoke the category by sending a log command:

LogManager.sharedInstance.log(#perform)

enter image description here

I am questioning what’s one of the best method to take right here?
Earlier than Swift Concurrency and if I take away MainActor from the SysInfo, all of it works as if it is synchronous.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments