I am presenting a list of Items which are sections by the categoryIdentifier property. My app uses CloudKit. When I add change the categoryIdentifier for an item it is display in the correct section and other running instances of the app do the same. When restarting the app some items are grouped under an incorrect section, but the cstegoryIdentifier within the item is still as it was set. My question is what I'm doing wrong that upon restart the organization is incorrect. In case it matters, I'm setting this in the container:
container.viewContext.automaticallyMergesChangesFromParent = true
As an aside: It seems necessary to make the sectioning type optional (as is the case in the underlying entity) like this
SectionedFetchResults<String?, Item>
Though the examples don't seem to need this.
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@State private var isShowingItem = false
@State private var isAddingItem = false
@SectionedFetchRequest(
sectionIdentifier: \.categoryIdentifier,
sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: true)],
animation: .default) private var sectionedItems: SectionedFetchResults<String?, Item>
var body: some View {
NavigationView {
List {
ForEach(sectionedItems) { section in
Section(header: Text(Category.nameForId(section.id, context: viewContext)).font(.headline)) {
ForEach(section) { item in
NavigationLink(destination: ItemInputView(item: item, category: Category.get(identifier: item.category, context: viewContext))) {
Text(item.getName())
}
}
}
}
}
.navigationTitle("Foo")
.sheet(isPresented: $isAddingItem) {
ItemInputView(item: nil, category: Category.getDefault(context: viewContext))
}
}
.navigationViewStyle(.stack)
}
}
Post
Replies
Boosts
Views
Activity
Hi, after rotating the iPad ARCamera.trackingState transitions to .initilizing and the video freezes. The application as a whole still functions. I'm unable to extract a relevant code example as this is embedded in a larger application. I am not seeing this behavior on iPhone. Is there any possibility that this is related to layout constraints? I see some warnings when the rotation occurs. Is there anything else I might examine?
Thank you
Hi, why doesn't this view scroll? The image is taller than the screen.
struct ImageScroll: View {
@State var uiImage: UIImage
var body: some View {
ScrollView {
Image(uiImage: uiImage)
.focusable()
}
.focusable()
}
}
Thanks,
Spiff
Hi, is it possible to disable scrolling behavior in the SwiftUI List view? I'd like to take advantage of the new grouping features
List(content, children: \.children)
in List and want the list to be part of a larger scrolling view. As it stands I get an embedded scroll view for the list which is not my intent.
Thanks!
I'm trying to render a MKPolyline on tvOS and get a runtime exception with
_validateTextureView:531: failed assertion `cannot create View from Memoryless texture.'
This same code works fine on the iPad and I'm starting to think it just doesn't work on tvOS...unless this is a beta issue.
Hi, I've constructed a NWListener closely based on the example provided here: https://developer.apple.com/forums/thread/653925?login=true&page=1#621284022
The problem is that the example specifically only handles a single connection request and then ignores all others. I've removed that logic. The problem is that for
code running on two simulators (tvOS and iOS) I get four accepts each time I connect from the client. They are coming in pairs of ipv4/ipv6. The second address might be the simulator,.0.5 is the mac. I need to implement a server that accepts as many connections over time as needed. My questions are
a) can I only listen on one protocol, for example ipv6 only?
b) if not how should I ignore the other connection? I don't seem to get information into newConnectionHandler, which is too late.
c) why am I getting a second pair of connections?
Thanks in advance for your help!
Cliff
func startListener() {
				do {
						let tcpOption = NWProtocolTCP.Options()
						tcpOption.enableKeepalive = true
						tcpOption.keepaliveIdle = 2
						
						let params = NWParameters(tls: nil, tcp: tcpOption)
						params.includePeerToPeer = true
						
						let listener = try NWListener(using: params)
						networkListener = listener
						listener.service = NWListener.Service(name: NetworkConstants.serviceName, type: NetworkConstants.serviceType)
						listener.newConnectionLimit = NWListener.InfiniteConnectionLimit
						
						listener.stateUpdateHandler = { [weak self] newState in
								guard let strongSelf = self else { return }
								switch newState {
								case .ready:
										let listenerMessage = "listening on \(NetworkConstants.serviceName) \(NetworkConstants.serviceType) \(String(describing: strongSelf.networkListener!.port))"
										strongSelf.networkDelegate?.didReceiveListenerUpdate(listener: strongSelf, didUpdateMessage: listenerMessage)
								case .failed(let error):
										strongSelf.networkListener?.cancel()
										if strongSelf.didListenerFail {
												print("listener did fail")
												strongSelf.didListenerFail = true
												NetworkListener.shared.startListener()
										} else {
												let errorMessage = "Listener - failed with \(error.localizedDescription), restarting"
												strongSelf.networkDelegate?.didReceiveListenerUpdate(listener: strongSelf, didUpdateMessage: errorMessage)
										}
								default:
										print("listener: unhandled state \(newState)")
										break
								}
						}
						
						listener.newConnectionHandler = { [weak self] newConnection in
								guard let strongSelf = self else { return }
								strongSelf.networkDelegate?.didReceiveListenerUpdate(listener: strongSelf, didUpdateMessage: "Listener received a new connection")
								strongSelf.networkDelegate?.didReceiveNewConnection(listener: strongSelf, newConnection: newConnection)
						}
						
						listener.start(queue: .main)
				} catch {
						print("Can't make listener: \(error.localizedDescription)")
				}
		}
listener: ScreenshotServer.NetworkListener: Listener received a new connection
listener: ScreenshotServer.NetworkListener: new connection [C1 ::1.52396 tcp, local: ::1.52309, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]
listener: ScreenshotServer.NetworkListener: Listener received a new connection
listener: ScreenshotServer.NetworkListener: new connection [C2 192.168.0.5:52397 tcp, local: 192.168.0.5:52309, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]
listener: ScreenshotServer.NetworkListener: Listener received a new connection
listener: ScreenshotServer.NetworkListener: new connection [C3 192.168.0.2:52398 tcp, local: 192.168.0.2:52309, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]
listener: ScreenshotServer.NetworkListener: Listener received a new connection
listener: ScreenshotServer.NetworkListener: new connection [C4 169.254.240.25:52399 tcp, local: 169.254.240.25:52309, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]
I’m pulling out my hair trying to reverse-engineer the more-or-less undocumented Network framework. It appears that two things are happening, one is that the listener is accepting two connection requests, one on ipv4 and one on ipv6. Secondly, it seems that the the connection immediately sets isComplete.
Also, what does prohibit joining mean in the connection?
server: did accept connection: [C1 ::1.63423 tcp, local: ::1.63406, server, prohibit joining, path satisfied (Path is satisfied), interface: lo0]
It sure would be nice to have more examples of this framework in action, it feels like Apple is not really committed to it and that worries me.
class Server {
		var publisher = PassthroughSubject<Data, NWError>()
		
		private var connection: NWConnection? = nil
		let listener: NWListener
		
		init() {
				listener = try! NWListener(using: .tcp)
		}
		
		func report(_ msg: String) {
				print("server: \(msg)")
		}
		
		func start() throws {
				report("start server")
				
				listener.service = NWListener.Service(name: bonjourName, type: bonjourService, domain: nil)
				listener.stateUpdateHandler = self.stateDidChange(to:)
				listener.newConnectionHandler = self.didAccept(nwConnection:)
				listener.serviceRegistrationUpdateHandler = self.serviceRegistrationUpdateHandler
				listener.start(queue: .main)
		}
		
		private func stateDidChange(to newState: NWListener.State) {
				switch newState {
				case .ready:
						report("Server ready service=\(listener.service) port=\(listener.port)")
				case .failed(let error):
						report("Server failure, error: \(error.localizedDescription)")
						publisher.send(completion: Subscribers.Completion.failure(error))
				default:
						break
				}
		}
		
		private func serviceRegistrationUpdateHandler(_ change: NWListener.ServiceRegistrationChange) {
				report("registration did change: \(change)")
		}
		
		private func didAccept(nwConnection: NWConnection) {
				report("did accept connection: \(nwConnection)")
				connection = nwConnection
				connection?.stateUpdateHandler = self.stateDidChange
				connection?.start(queue: .main)
				receive()
		}
		private func stateDidChange(to state: NWConnection.State) {
				switch state {
				case .waiting(let error):
						publisher.send(completion: Subscribers.Completion.failure(error))
				case .ready:
						report("connection ready")
				case .failed(let error):
						publisher.send(completion: Subscribers.Completion.failure(error))
						connectionDidFail(error: error)
				default:
						break
				}
		}
		
		// functions removed to make space
		
		private func receive() {
				connection?.receive(minimumIncompleteLength: 1, maximumLength: Int.max) { (data, _, isComplete, error) in
						if let error = error {
								self.report("connection failed: \(error)")
								self.connectionDidFail(error: error)
								return
						}
						
						if let data = data, !data.isEmpty {
								self.report("connection did receive, data: \(data)")
								self.publisher.send(data)
						}
						if isComplete {
								self.report("is complete")
//								self.connectionDidEnd()
								return
						} else {
								self.report("setup next read")
								self.receive()
						}
				}
		}
	
		func send(data: Data) {
				report("connection will send data: \(data)")
				self.connection?.send(content: data, contentContext: .defaultStream, completion: .contentProcessed( { error in
						if let error = error {
								self.connectionDidFail(error: error)
								return
						}
						self.report("connection did send, data: \(data)")
				}))
		}
		
		func sendStreamOriented(connection: NWConnection, data: Data) {
				connection.send(content: data, completion: .contentProcessed({ error in
						if let error = error {
								self.connectionDidFail(error: error)
						}
				}))
		}
}