refactor: use weak reference

This commit is contained in:
dscyrescotti
2024-06-10 23:25:32 +07:00
parent 09b97da1fe
commit d983464402
3 changed files with 28 additions and 23 deletions
+26 -21
View File
@@ -49,17 +49,17 @@ final class GraphicContext: @unchecked Sendable {
case .marker: case .marker:
guard let penStroke = stroke.stroke(as: PenStroke.self) else { return } guard let penStroke = stroke.stroke(as: PenStroke.self) else { return }
let deletedStroke = tree.remove(penStroke.anyStroke, in: penStroke.strokeBox) let deletedStroke = tree.remove(penStroke.anyStroke, in: penStroke.strokeBox)
withPersistence(\.backgroundContext) { [stroke = deletedStroke] context in withPersistence(\.backgroundContext) { [weak penStroke] context in
stroke?.stroke(as: PenStroke.self)?.object?.graphicContext = nil penStroke?.object?.graphicContext = nil
try context.saveIfNeeded() try context.saveIfNeeded()
context.refreshAllObjects() context.refreshAllObjects()
} }
case .eraser: case .eraser:
guard let eraserStroke = stroke.stroke(as: EraserStroke.self) else { return } guard let eraserStroke = stroke.stroke(as: EraserStroke.self) else { return }
eraserStrokes.remove(eraserStroke) eraserStrokes.remove(eraserStroke)
let penStrokes = eraserStroke.penStrokes withPersistence(\.backgroundContext) { [weak eraserStroke] context in
withPersistence(\.backgroundContext) { [penStrokes] context in guard let eraserStroke else { return }
for penStroke in penStrokes { for penStroke in eraserStroke.penStrokes {
penStroke.eraserStrokes.remove(eraserStroke) penStroke.eraserStrokes.remove(eraserStroke)
if let object = eraserStroke.object { if let object = eraserStroke.object {
penStroke.object?.erasers.remove(object) penStroke.object?.erasers.remove(object)
@@ -82,8 +82,8 @@ final class GraphicContext: @unchecked Sendable {
break break
} }
tree.insert(penStroke.anyStroke, in: penStroke.strokeBox) tree.insert(penStroke.anyStroke, in: penStroke.strokeBox)
withPersistence(\.backgroundContext) { [weak self, penStroke] context in withPersistence(\.backgroundContext) { [weak self, weak penStroke] context in
penStroke.object?.graphicContext = self?.object penStroke?.object?.graphicContext = self?.object
try context.saveIfNeeded() try context.saveIfNeeded()
context.refreshAllObjects() context.refreshAllObjects()
} }
@@ -92,9 +92,9 @@ final class GraphicContext: @unchecked Sendable {
break break
} }
eraserStrokes.insert(eraserStroke) eraserStrokes.insert(eraserStroke)
let penStrokes = eraserStroke.penStrokes withPersistence(\.backgroundContext) { [weak eraserStroke] context in
withPersistence(\.backgroundContext) { [eraserStroke, penStrokes] context in guard let eraserStroke else { return }
for penStroke in penStrokes { for penStroke in eraserStroke.penStrokes {
penStroke.eraserStrokes.insert(eraserStroke) penStroke.eraserStrokes.insert(eraserStroke)
if let object = eraserStroke.object { if let object = eraserStroke.object {
penStroke.object?.erasers.add(object) penStroke.object?.erasers.add(object)
@@ -122,16 +122,16 @@ extension GraphicContext {
let id = stroke.objectID let id = stroke.objectID
queue.addOperation { [weak self] in queue.addOperation { [weak self] in
guard let self else { return } guard let self else { return }
withPersistenceSync(\.newBackgroundContext) { [_stroke] context in withPersistenceSync(\.newBackgroundContext) { [weak _stroke] context in
guard let stroke = try? context.existingObject(with: id) as? StrokeObject else { return } guard let stroke = try? context.existingObject(with: id) as? StrokeObject else { return }
_stroke.loadQuads(from: stroke, with: self) _stroke?.loadQuads(from: stroke, with: self)
context.refreshAllObjects() context.refreshAllObjects()
} }
} }
} else { } else {
withPersistence(\.backgroundContext) { [weak self] context in withPersistence(\.backgroundContext) { [weak self, weak _stroke] context in
guard let self else { return } guard let self else { return }
_stroke.loadQuads(with: self) _stroke?.loadQuads(with: self)
context.refreshAllObjects() context.refreshAllObjects()
} }
} }
@@ -175,7 +175,8 @@ extension GraphicContext {
createdAt: .now, createdAt: .now,
thickness: pen.thickness thickness: pen.thickness
) )
withPersistence(\.backgroundContext) { [graphicContext = object, _stroke = penStroke] context in withPersistence(\.backgroundContext) { [weak graphicContext = object, weak _stroke = penStroke] context in
guard let _stroke else { return }
let stroke = StrokeObject(\.backgroundContext) let stroke = StrokeObject(\.backgroundContext)
stroke.bounds = _stroke.bounds stroke.bounds = _stroke.bounds
stroke.color = _stroke.color stroke.color = _stroke.color
@@ -199,7 +200,8 @@ extension GraphicContext {
thickness: pen.thickness thickness: pen.thickness
) )
eraserStroke.graphicContext = self eraserStroke.graphicContext = self
withPersistence(\.backgroundContext) { [_stroke = eraserStroke] context in withPersistence(\.backgroundContext) { [weak _stroke = eraserStroke] context in
guard let _stroke else { return }
let stroke = EraserObject(\.backgroundContext) let stroke = EraserObject(\.backgroundContext)
stroke.bounds = _stroke.bounds stroke.bounds = _stroke.bounds
stroke.color = _stroke.color stroke.color = _stroke.color
@@ -234,7 +236,8 @@ extension GraphicContext {
if let penStroke = currentStroke.stroke(as: PenStroke.self) { if let penStroke = currentStroke.stroke(as: PenStroke.self) {
penStroke.saveQuads() penStroke.saveQuads()
tree.insert(currentStroke.anyStroke, in: currentStroke.strokeBox) tree.insert(currentStroke.anyStroke, in: currentStroke.strokeBox)
withPersistence(\.backgroundContext) { [penStroke] context in withPersistence(\.backgroundContext) { [weak penStroke] context in
guard let penStroke else { return }
penStroke.object?.bounds = penStroke.bounds penStroke.object?.bounds = penStroke.bounds
try context.saveIfNeeded() try context.saveIfNeeded()
context.refreshAllObjects() context.refreshAllObjects()
@@ -242,7 +245,8 @@ extension GraphicContext {
} else if let eraserStroke = currentStroke.stroke(as: EraserStroke.self) { } else if let eraserStroke = currentStroke.stroke(as: EraserStroke.self) {
eraserStroke.saveQuads() eraserStroke.saveQuads()
eraserStrokes.insert(eraserStroke) eraserStrokes.insert(eraserStroke)
withPersistence(\.backgroundContext) { [eraserStroke] context in withPersistence(\.backgroundContext) { [weak eraserStroke] context in
guard let eraserStroke else { return }
eraserStroke.object?.bounds = eraserStroke.bounds eraserStroke.object?.bounds = eraserStroke.bounds
try context.saveIfNeeded() try context.saveIfNeeded()
context.refreshAllObjects() context.refreshAllObjects()
@@ -258,7 +262,8 @@ extension GraphicContext {
switch stroke.style { switch stroke.style {
case .marker: case .marker:
guard let _stroke = stroke.stroke(as: PenStroke.self) else { break } guard let _stroke = stroke.stroke(as: PenStroke.self) else { break }
withPersistence(\.backgroundContext) { [graphicContext = object, _stroke] context in withPersistence(\.backgroundContext) { [weak graphicContext = object, weak _stroke] context in
guard let _stroke else { return }
if let stroke = _stroke.object { if let stroke = _stroke.object {
graphicContext?.strokes.remove(stroke) graphicContext?.strokes.remove(stroke)
context.delete(stroke) context.delete(stroke)
@@ -268,8 +273,8 @@ extension GraphicContext {
case .eraser: case .eraser:
guard let eraserStroke = stroke.stroke(as: EraserStroke.self) else { break } guard let eraserStroke = stroke.stroke(as: EraserStroke.self) else { break }
eraserStrokes.remove(eraserStroke) eraserStrokes.remove(eraserStroke)
withPersistence(\.backgroundContext) { [eraserStroke] context in withPersistence(\.backgroundContext) { [weak eraserStroke] context in
if let stroke = eraserStroke.object { if let stroke = eraserStroke?.object {
context.delete(stroke) context.delete(stroke)
} }
try context.saveIfNeeded() try context.saveIfNeeded()
@@ -98,7 +98,7 @@ final class EraserStroke: Stroke, @unchecked Sendable {
let endIndex = endIndex ?? quads.endIndex let endIndex = endIndex ?? quads.endIndex
let batch = quads[batchIndex..<endIndex] let batch = quads[batchIndex..<endIndex]
batchIndex = endIndex batchIndex = endIndex
withPersistence(\.backgroundContext) { [weak self, eraser = object, quads = batch] context in withPersistence(\.backgroundContext) { [weak self, weak eraser = object, quads = batch] context in
guard let self, let eraser else { return } guard let self, let eraser else { return }
for _quad in quads { for _quad in quads {
let quad = QuadObject(\.backgroundContext) let quad = QuadObject(\.backgroundContext)
@@ -134,7 +134,7 @@ final class PenStroke: Stroke, @unchecked Sendable {
let endIndex = endIndex ?? quads.endIndex let endIndex = endIndex ?? quads.endIndex
let batch = quads[batchIndex..<endIndex] let batch = quads[batchIndex..<endIndex]
batchIndex = endIndex batchIndex = endIndex
withPersistence(\.backgroundContext) { [object, quads = batch] context in withPersistence(\.backgroundContext) { [weak object, quads = batch] context in
for _quad in quads { for _quad in quads {
let quad = QuadObject(\.backgroundContext) let quad = QuadObject(\.backgroundContext)
quad.originX = _quad.originX.cgFloat quad.originX = _quad.originX.cgFloat