Post

Replies

Boosts

Views

Activity

Problems when using @Binding on a dynamic array to edit a list (and the items it contains)
I'm running into an issue where, eventually, something goes "wonky" on some detail items won't present the DetailView when they are clicked on. At first, everything is fine, I can edit (and see results reflected in realtime). I can reorder the list, and I can add Detail items to the list. However, after a while, I start seeing the default detail view (Text("Select a detail item to view")) whenever I click on some items. I've not been able to predict where a failure will occur. In the real app I have a persisted JSON document that never seems to get corrupted. And reflects the edits I perform. With this sample app, I can pretty reliably trigger the issue by moving a detail item in the list, and then adding a detail item, but, it will eventually fail without those actions. Am I doing something that's "forbidden"? (You can create a new app and replace the ContentView.swift with the code below to run this and play with it) import SwiftUI struct ContentView: View { @State var main = Main() var body: some View { NavView(main: $main) .onAppear { for index in 1..<11 { main.details.append( Detail( title: "Detail \(index)", description: "Description \(index)")) } } } } struct NavView: View { @Binding var main: Main var body: some View { NavigationSplitView { ListView(main: $main) } detail: { Text("Select a detail item to view") } } } struct ListView: View { @Binding var main: Main @State private var addedDetailCount = 0 var body: some View { List($main.details, editActions: .move) { $detail in NavigationLink(destination: DetailView(detail: $detail)) { Text("\(detail.title)") } } .toolbar { Button( LocalizedStringKey("Add Detail"), systemImage: "plus" ) { addedDetailCount += 1 main.details.append( .init(title: "new Detail \(addedDetailCount)", description: "description")) } } } } struct DetailView: View { @Binding var detail: Detail var body: some View { Form { Text("id: \(detail.id)") TextField("Title", text: $detail.title) TextField("Detail", text: $detail.description) } .padding() } } struct Main { var details = [Detail]() } struct Detail: Identifiable { var id = UUID() var title: String var description: String } #Preview { ContentView() }
3
0
164
1w
"IOSurface creation failed" drawing to CGContext
(more details on StackOverflow) I'm getting messages like the following, SOMETIMES, when I draw to a CGContext IOSurface creation failed: e00002c2 parentID: 00000000 properties: { IOSurfaceAddress = 5207703552; IOSurfaceAllocSize = 9461418; IOSurfaceCacheMode = 0; IOSurfaceName = CMPhoto; IOSurfacePixelFormat = 1246774599; } call to context.draw(): context.draw(photo.image, in: CGRect(x: 0, y: top, width: width, height: height), byTiling: false) The results are just fine, so the draw seems to be working. It also, most often, draws without producing this error, but it fails pretty often. I'm not sure where to begin looking to sort out what I might need to do differently to avoid this error message in the console. Complete code: import Foundation import SwiftUI func generateSpritesImage(thumbPhotos: [Photo], width: Int, filename: URL) -> [Int] { var indices = [Int]() let totalHeight = thumbPhotos.reduce(0) { $0 + $1.heightOfImage(ofWidth: width) } debugPrint("creating context") let context = CGContext(data: nil, width: width, height: totalHeight, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)! var top = totalHeight for photo in thumbPhotos { let height = photo.heightOfImage(ofWidth: width) indices.append(top - totalHeight) top -= height debugPrint("drawing \(photo.filteredFileURL())") context.draw(photo.image, in: CGRect(x: 0, y: top, width: width, height: height), byTiling: false) } debugPrint("write jpeg") writeJpegFromContext(context: context, filename: filename) return indices } func writeJpegFromContext(context: CGContext, filename: URL) { let cgImage = context.makeImage()! let bitmapRep = NSBitmapImageRep(cgImage: cgImage) let jpegData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])! try! jpegData.write(to: filename) } sample of output: "drawing 0002-_MG_8542.jpg" "drawing 0003-_MG_8545.jpg" "drawing 0004-_MG_8550.jpg" IOSurface creation failed: e00002c2 parentID: 00000000 properties: { IOSurfaceAddress = 5211357184; IOSurfaceAllocSize = 9983331; IOSurfaceCacheMode = 0; IOSurfaceName = CMPhoto; IOSurfacePixelFormat = 1246774599; } "drawing 0005-_MG_8555.jpg" IOSurface creation failed: e00002c2 parentID: 00000000 properties: { IOSurfaceAddress = 5221351424; IOSurfaceAllocSize = 10041215; IOSurfaceCacheMode = 0; IOSurfaceName = CMPhoto; IOSurfacePixelFormat = 1246774599; } "drawing 0006-_MG_8562.jpg" "drawing 0007-_MG_8563.jpg" IOSurface creation failed: e00002c2 parentID: 00000000 properties: { IOSurfaceAddress = 5376163840; IOSurfaceAllocSize = 10109756; IOSurfaceCacheMode = 0; IOSurfaceName = CMPhoto; IOSurfacePixelFormat = 1246774599; } "drawing 0008-_MG_8584.jpg" "drawing 0009-_MG_8618.jpg" IOSurface creation failed: e00002c2 parentID: 00000000 properties: { IOSurfaceAddress = 5394612224; IOSurfaceAllocSize = 8425564; IOSurfaceCacheMode = 0; IOSurfaceName = CMPhoto; IOSurfacePixelFormat = 1246774599; } "drawing 0010-_MG_8627.jpg" "drawing 0011-_MG_8649.jpg" "drawing 0012-_MG_8658.jpg" "drawing 0013-_MG_8665.jpg" "drawing 0014-_MG_8677.jpg" "drawing 0015-_MG_8675.jpg" "drawing 0016-_MG_8676.jpg" "drawing 0017-IMGP0873.jpg" "drawing 0018-_MG_8719.jpg" "drawing 0019-_MG_8743.jpg" ...
3
0
1.3k
Jan ’24