Created
September 26, 2025 14:55
-
-
Save pookjw/170eb5d394ed7936e2948af8c1fb5e56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import CoreData | |
| let model = NSManagedObjectModel() | |
| let entity = NSEntityDescription() | |
| entity.name = "Entity" | |
| model.entities = [entity] | |
| let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model) | |
| let storeURL = URL.temporaryDirectory.appending(component: UUID().uuidString).appendingPathExtension("sqlite") | |
| let description = NSPersistentStoreDescription(url: storeURL) | |
| // 안해도 됨 | |
| description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) | |
| description.shouldAddStoreAsynchronously = false | |
| coordinator.addPersistentStore(with: description) { _, error in | |
| assert(error == nil) | |
| } | |
| let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) | |
| context.persistentStoreCoordinator = coordinator | |
| func createObjects(count: Int) { | |
| for _ in 0..<count { | |
| _ = NSManagedObject(entity: entity, insertInto: context) | |
| } | |
| try! context.save() | |
| } | |
| let flag = false | |
| if flag { | |
| try context.setQueryGenerationFrom(.current) | |
| createObjects(count: 10) | |
| let count = try! context.count(for: NSFetchRequest(entityName: entity.name!)) | |
| assert(count == 0) | |
| } else { | |
| try context.setQueryGenerationFrom(.current) | |
| try context.save() | |
| let query = context.queryGenerationToken! | |
| createObjects(count: 10) | |
| try context.setQueryGenerationFrom(query) | |
| let count = try! context.count(for: NSFetchRequest(entityName: entity.name!)) | |
| assert(count == 0) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment