Tuesday, March 26, 2024
HomeiOS Developmentios - Easy methods to get an situations of saved realm objects...

ios – Easy methods to get an situations of saved realm objects in my SwiftUI DeviceActivityMonitorExtension level within the background?


I’ve initialized realm within the init of the @primary app level like this:

init() {
        
    let fileURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier:             "group.com.identifier")!                     
            .appendingPathComponent("default.realm")
        
    let config = Realm.Configuration(fileURL: fileURL)
        
        // Set this configuration because the default for Realm throughout the app
    Realm.Configuration.defaultConfiguration = config
}

And i’ve a realm knowledge supervisor like this:

class SetupDataManager {
    static let shared = SetupDataManager()
    
    personal var realm: Realm {
        get {
            return attempt! Realm() 
        }
    }
    
    personal init() {}

    func fetchBalancedActivityModel(by id: String) async -> BalancedActivityModel? {
        return realm.object(ofType: BalancedActivityModel.self, forPrimaryKey: id)
    }
}

And these work nicely sufficient for when the app is energetic and that i need to save and fetch the balancedActivityModels and even use @ObservedResults(BalancedActivityModel.self) var balancedActionItems in my views.

Nevertheless i am additionally utilizing the DeviceActivity api and that i need to fetch these balancedActivityModels in my DeviceActivityMonitorExtension and particularly when the operate eventDidReachThreshold will get referred to as.

Now i am confused about easy methods to initialize one other realm and thread to go along with it and entry the saved fashions within the background.. presently i’ve the operate carried out like this however this actually does not work

    override func eventDidReachThreshold(_ occasion: DeviceActivityEvent.Title, exercise: DeviceActivityName) {
        tremendous.eventDidReachThreshold(occasion, exercise: exercise)
        let fileURL = FileManager.default
            .containerURL(forSecurityApplicationGroupIdentifier: "group.com.identifier")!
            .appendingPathComponent("default.realm")
        let config = Realm.Configuration(fileURL: fileURL)
        Realm.Configuration.defaultConfiguration = config
        
        let realm = attempt! Realm()
        
        guard let balancedActivityModel = realm.object(ofType: BalancedActivityModel.self, forPrimaryKey: exercise.rawValue) else {
            return
        }
        
        .. remainder of the code like
        realm.writeAsync {
            balancedActivityModel.timeIsUp = true
            balancedActivityModel.depend += 1
        }
        
        does not work..
    }

Any suggestions appreciated!

Initialize new realm from extension..



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments