//
//  NSManagedContext+Extensions.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 24/01/2023.
//

import Foundation
import CoreData

extension NSManagedObjectContext {
    
    func object(stringId: String) -> NSManagedObject? {
        guard let url = URL(string: stringId) else { return nil }
        guard let objectId = PersistenceController.shared.container.persistentStoreCoordinator.managedObjectID(forURIRepresentation: url) else { return nil }
        return self.object(with: objectId)
    }
    
}

extension NSManagedObject {
    
    var isTemporary: Bool {
        return self.objectID.isTemporaryID
    }
    
    var stringId: String {
        return self.objectID.uriRepresentation().absoluteString
    }
    
}
