mirror of
https://github.com/dscyrescotti/Memola.git
synced 2026-03-24 18:31:23 +01:00
feat: load quads using new background context
This commit is contained in:
@@ -62,13 +62,21 @@ final class GraphicContext: @unchecked Sendable {
|
||||
extension GraphicContext {
|
||||
func loadStrokes(_ bounds: CGRect) {
|
||||
guard let object else { return }
|
||||
let queue = OperationQueue()
|
||||
queue.qualityOfService = .userInteractive
|
||||
self.strokes = object.strokes.compactMap { stroke -> Stroke? in
|
||||
guard let stroke = stroke as? StrokeObject else { return nil }
|
||||
let _stroke = Stroke(object: stroke)
|
||||
if _stroke.isVisible(in: bounds) {
|
||||
_stroke.loadQuads()
|
||||
withPersistence(\.backgroundContext) { [stroke] context in
|
||||
context.refresh(stroke, mergeChanges: false)
|
||||
let id = stroke.objectID
|
||||
queue.addOperation {
|
||||
withPersistenceSync(\.newBackgroundContext) { [_stroke] context in
|
||||
guard let stroke = try? context.existingObject(with: id) as? StrokeObject else { return }
|
||||
_stroke.loadQuads(from: stroke)
|
||||
}
|
||||
withPersistence(\.backgroundContext) { [stroke] context in
|
||||
context.refresh(stroke, mergeChanges: false)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
withPersistence(\.backgroundContext) { [stroke] context in
|
||||
@@ -78,6 +86,7 @@ extension GraphicContext {
|
||||
}
|
||||
return _stroke
|
||||
}
|
||||
queue.waitUntilAllOperationsAreFinished()
|
||||
}
|
||||
|
||||
func loadQuads(_ bounds: CGRect) {
|
||||
|
||||
@@ -98,6 +98,13 @@ extension Stroke {
|
||||
}
|
||||
}
|
||||
|
||||
func loadQuads(from object: StrokeObject) {
|
||||
quads = object.quads.compactMap { quad in
|
||||
guard let quad = quad as? QuadObject else { return nil }
|
||||
return Quad(object: quad)
|
||||
}
|
||||
}
|
||||
|
||||
func addQuad(at point: CGPoint, rotation: CGFloat, shape: QuadShape) -> Quad {
|
||||
let quad = Quad(
|
||||
origin: point,
|
||||
|
||||
@@ -26,6 +26,13 @@ final class Persistence {
|
||||
return context
|
||||
}()
|
||||
|
||||
var newBackgroundContext: NSManagedObjectContext {
|
||||
let context = persistentContainer.newBackgroundContext()
|
||||
context.undoManager = nil
|
||||
context.automaticallyMergesChangesFromParent = true
|
||||
return context
|
||||
}
|
||||
|
||||
lazy var persistentContainer: NSPersistentContainer = {
|
||||
let persistentStore = NSPersistentStoreDescription()
|
||||
persistentStore.shouldMigrateStoreAutomatically = true
|
||||
@@ -83,3 +90,14 @@ func withPersistence(_ keypath: KeyPath<Persistence, NSManagedObjectContext>, _
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withPersistenceSync(_ keypath: KeyPath<Persistence, NSManagedObjectContext>, _ task: @escaping (NSManagedObjectContext) throws -> Void) {
|
||||
let context = Persistence.shared[keyPath: keypath]
|
||||
context.performAndWait {
|
||||
do {
|
||||
try task(context)
|
||||
} catch {
|
||||
NSLog("[Memola] - \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user