feat: save quads by batch

This commit is contained in:
dscyrescotti
2024-06-07 19:59:29 +07:00
parent 2c01147af5
commit e9708ae072
2 changed files with 25 additions and 10 deletions
@@ -160,6 +160,7 @@ extension GraphicContext {
guard currentPoint != nil, let currentStroke = currentStroke?.stroke(as: PenStroke.self) else { return } guard currentPoint != nil, let currentStroke = currentStroke?.stroke(as: PenStroke.self) else { return }
currentStroke.finish(at: point) currentStroke.finish(at: point)
tree.insert(currentStroke.anyStroke, in: currentStroke.strokeBox) tree.insert(currentStroke.anyStroke, in: currentStroke.strokeBox)
currentStroke.saveQuads()
withPersistence(\.backgroundContext) { [currentStroke] context in withPersistence(\.backgroundContext) { [currentStroke] context in
guard let stroke = currentStroke.stroke(as: PenStroke.self) else { return } guard let stroke = currentStroke.stroke(as: PenStroke.self) else { return }
stroke.object?.bounds = stroke.bounds stroke.object?.bounds = stroke.bounds
@@ -28,6 +28,9 @@ final class PenStroke: Stroke, @unchecked Sendable {
var object: StrokeObject? var object: StrokeObject?
let batchSize: Int = 50
var batchIndex: Int = 0
init( init(
bounds: [CGFloat], bounds: [CGFloat],
color: [CGFloat], color: [CGFloat],
@@ -84,7 +87,17 @@ final class PenStroke: Stroke, @unchecked Sendable {
max(quad.originX.cgFloat, bounds[2]), max(quad.originX.cgFloat, bounds[2]),
max(quad.originY.cgFloat, bounds[3]) max(quad.originY.cgFloat, bounds[3])
] ]
withPersistence(\.backgroundContext) { [object, _quad = quad] context in if quads.endIndex >= batchIndex + batchSize {
saveQuads(to: batchIndex + batchSize)
}
}
func saveQuads(to endIndex: Int? = nil) {
let endIndex = endIndex ?? quads.endIndex
let batch = quads[batchIndex..<endIndex]
batchIndex = endIndex
withPersistence(\.backgroundContext) { [object, quads = batch] context in
for _quad in quads {
let quad = QuadObject(\.backgroundContext) let quad = QuadObject(\.backgroundContext)
quad.originX = _quad.originX.cgFloat quad.originX = _quad.originX.cgFloat
quad.originY = _quad.originY.cgFloat quad.originY = _quad.originY.cgFloat
@@ -97,3 +110,4 @@ final class PenStroke: Stroke, @unchecked Sendable {
} }
} }
} }
}