Skip to content

Instantly share code, notes, and snippets.

@pookjw
Created September 26, 2025 14:52
Show Gist options
  • Select an option

  • Save pookjw/8b1c7a6766c34dfd955452d758045c99 to your computer and use it in GitHub Desktop.

Select an option

Save pookjw/8b1c7a6766c34dfd955452d758045c99 to your computer and use it in GitHub Desktop.
import CoreData
class Delegate: NSObject, NSFetchedResultsControllerDelegate {
func controller(_ controller: NSFetchedResultsController<any NSFetchRequestResult>, didChangeContentWith diff: CollectionDifference<NSManagedObjectID>) {
}
}
var delegate: Delegate!
var fetchedResultsController: NSFetchedResultsController<NSManagedObject>!
var token_1: (any NSObjectProtocol)!
var token_2: (any NSObjectProtocol)!
@main
struct MyScript {
static func main() throws {
let model = NSManagedObjectModel()
let entity = NSEntityDescription()
entity.name = "Entity"
let attribute = NSAttributeDescription()
attribute.name = "attribute"
attribute.type = .integer16
entity.properties = [attribute]
model.entities = [entity]
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
let storeURL = URL.temporaryDirectory.appending(component: UUID().uuidString).appendingPathExtension("sqlite")
let description = NSPersistentStoreDescription(url: storeURL)
description.shouldAddStoreAsynchronously = false
coordinator.addPersistentStore(with: description) { _, error in
assert(error == nil)
}
let mainContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
mainContext.persistentStoreCoordinator = coordinator
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: entity.name!)
fetchRequest.sortDescriptors = [NSSortDescriptor(key: attribute.name, ascending: true)]
fetchedResultsController = NSFetchedResultsController<NSManagedObject>(
fetchRequest: fetchRequest,
managedObjectContext: mainContext,
sectionNameKeyPath: nil,
cacheName: nil
)
delegate = Delegate()
fetchedResultsController.delegate = delegate
try fetchedResultsController.performFetch()
let block = {
try! coordinator.performAndWait {
let backgroundContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
backgroundContext.persistentStoreCoordinator = coordinator
try backgroundContext.performAndWait {
let object = NSManagedObject(entity: entity, insertInto: backgroundContext)
try backgroundContext.setQueryGenerationFrom(.current)
try backgroundContext.save()
}
}
}
token_1 = NotificationCenter.default.addObserver(forName: NSManagedObjectContext.didSaveObjectsNotification, object: nil, queue: nil) { _ in
NotificationCenter.default.removeObserver(token_1)
block()
}
token_2 = NotificationCenter.default.addObserver(forName: NSManagedObjectContext.didSaveObjectsNotification, object: nil, queue: .main) { _ in
NotificationCenter.default.removeObserver(token_2)
block()
}
do {
let backgroundContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
backgroundContext.persistentStoreCoordinator = coordinator
backgroundContext.perform {
let object = NSManagedObject(entity: entity, insertInto: backgroundContext)
try! mainContext.save()
}
}
CFRunLoopRun()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment