mirror of
https://github.com/dscyrescotti/Memola.git
synced 2026-04-25 10:08:34 +02:00
feat: load quads using new background context
This commit is contained in:
@@ -62,13 +62,21 @@ final class GraphicContext: @unchecked Sendable {
|
|||||||
extension GraphicContext {
|
extension GraphicContext {
|
||||||
func loadStrokes(_ bounds: CGRect) {
|
func loadStrokes(_ bounds: CGRect) {
|
||||||
guard let object else { return }
|
guard let object else { return }
|
||||||
|
let queue = OperationQueue()
|
||||||
|
queue.qualityOfService = .userInteractive
|
||||||
self.strokes = object.strokes.compactMap { stroke -> Stroke? in
|
self.strokes = object.strokes.compactMap { stroke -> Stroke? in
|
||||||
guard let stroke = stroke as? StrokeObject else { return nil }
|
guard let stroke = stroke as? StrokeObject else { return nil }
|
||||||
let _stroke = Stroke(object: stroke)
|
let _stroke = Stroke(object: stroke)
|
||||||
if _stroke.isVisible(in: bounds) {
|
if _stroke.isVisible(in: bounds) {
|
||||||
_stroke.loadQuads()
|
let id = stroke.objectID
|
||||||
withPersistence(\.backgroundContext) { [stroke] context in
|
queue.addOperation {
|
||||||
context.refresh(stroke, mergeChanges: false)
|
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 {
|
} else {
|
||||||
withPersistence(\.backgroundContext) { [stroke] context in
|
withPersistence(\.backgroundContext) { [stroke] context in
|
||||||
@@ -78,6 +86,7 @@ extension GraphicContext {
|
|||||||
}
|
}
|
||||||
return _stroke
|
return _stroke
|
||||||
}
|
}
|
||||||
|
queue.waitUntilAllOperationsAreFinished()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadQuads(_ bounds: CGRect) {
|
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 {
|
func addQuad(at point: CGPoint, rotation: CGFloat, shape: QuadShape) -> Quad {
|
||||||
let quad = Quad(
|
let quad = Quad(
|
||||||
origin: point,
|
origin: point,
|
||||||
|
|||||||
@@ -26,6 +26,13 @@ final class Persistence {
|
|||||||
return context
|
return context
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
var newBackgroundContext: NSManagedObjectContext {
|
||||||
|
let context = persistentContainer.newBackgroundContext()
|
||||||
|
context.undoManager = nil
|
||||||
|
context.automaticallyMergesChangesFromParent = true
|
||||||
|
return context
|
||||||
|
}
|
||||||
|
|
||||||
lazy var persistentContainer: NSPersistentContainer = {
|
lazy var persistentContainer: NSPersistentContainer = {
|
||||||
let persistentStore = NSPersistentStoreDescription()
|
let persistentStore = NSPersistentStoreDescription()
|
||||||
persistentStore.shouldMigrateStoreAutomatically = true
|
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