diff --git a/MeetingNotes/MeetingNotesDocument.swift b/MeetingNotes/MeetingNotesDocument.swift index 7d2d878f..5d18a5c2 100644 --- a/MeetingNotes/MeetingNotesDocument.swift +++ b/MeetingNotes/MeetingNotesDocument.swift @@ -87,7 +87,7 @@ final class MeetingNotesDocument: ReferenceFileDocument { } syncedDocumentTrigger = doc.objectWillChange.sink { - Logger.syncflow.trace("\(self.id) ** objectWillChange **") + Logger.syncflow.trace("APPSYNC: \(self.id) ** objectWillChange **") self.objectWillChange.send() } } @@ -111,13 +111,18 @@ final class MeetingNotesDocument: ReferenceFileDocument { // Set the identifier of this document, external from the Automerge document. id = wrappedDocument.id // Then deserialize the Automerge document from the wrappers data. - doc = try Document(wrappedDocument.data) - Logger.document - .debug( - "Created Automerge doc of ID \(self.id, privacy: .public) from CBOR encoded data of \(wrappedDocument.data.count, privacy: .public) bytes" - ) - modelEncoder = AutomergeEncoder(doc: doc, strategy: .createWhenNeeded) - modelDecoder = AutomergeDecoder(doc: doc) + do { + doc = try Document(wrappedDocument.data) + Logger.document + .debug( + "Created Automerge doc of ID \(self.id, privacy: .public) from CBOR encoded data of \(wrappedDocument.data.count, privacy: .public) bytes" + ) + modelEncoder = AutomergeEncoder(doc: doc, strategy: .createWhenNeeded) + modelDecoder = AutomergeDecoder(doc: doc) + } catch { + Logger.document.error("Failed to construct Automerge doc from data: \(error.localizedDescription)") + throw error + } do { model = try modelDecoder.decode(MeetingNotesModel.self) } catch let DecodingError.dataCorrupted(context) { @@ -152,7 +157,7 @@ final class MeetingNotesDocument: ReferenceFileDocument { .throttle(for: 1.0, scheduler: DispatchQueue.main, latest: true) .receive(on: RunLoop.main) .sink { - Logger.syncflow.trace("\(self.id) ** objectWillChange (1 sec delay) **") + Logger.syncflow.trace("APPSYNC: \(self.id) ** Automerge document objectWillChange (1 sec delay) **") do { try self.getModelUpdates() } catch { @@ -197,14 +202,14 @@ final class MeetingNotesDocument: ReferenceFileDocument { /// Updates the Automerge document with the current value from the model. func storeModelUpdates() throws { - Logger.syncflow.debug("Storing model updates") + Logger.syncflow.debug("APPSYNC: Storing model updates") try modelEncoder.encode(model) self.objectWillChange.send() } /// Updates the model document with any changed values in the Automerge document. func getModelUpdates() throws { - Logger.syncflow.debug("Loading model updates") + Logger.syncflow.debug("APPSYNC: Loading model updates") model = try modelDecoder.decode(MeetingNotesModel.self) } diff --git a/MeetingNotes/Views/PeerSyncView.swift b/MeetingNotes/Views/PeerSyncView.swift index 02963982..b738433c 100644 --- a/MeetingNotes/Views/PeerSyncView.swift +++ b/MeetingNotes/Views/PeerSyncView.swift @@ -134,12 +134,16 @@ struct PeerSyncView: View { listenerState = state }) .task { + // NOTE: this task gets invoked on _every_ re-appearance of the view - kind of the async + // equivalent of .onAppear() {} closure structure. + // + // The result is this bit if repeatedly redundant, but covers the case where the app is + // first coming online and a default "???" value should be set whatever the inline default + // from the library can provide. Since this is an @AppStorage() setup, if there's a configured + // setting, this won't get hit and we're just waiting cycles with the check. if nameToDisplay == "???" { // no user default is setup, so load a default value from the library nameToDisplay = await peerToPeer.peerName - } else { - // overrides the library default name - await peerToPeer.setName(nameToDisplay) } } }