Hi Rob. Thanks for the help. I actually cross posted this to stackoverflow (https://stackoverflow.com/questions/79041095/my-swift-concurrency-is-no-longer-running-properly-and-its-screwing-up-my-swift/79041796) and forgot to close this question. Using a ModelActor solved this for me but I’ll take a closer look at your answer when I get a chance.
Post
Replies
Boosts
Views
Activity
Realized I didn't share the error: Thread 1: "NSToolbar 0x600003fa74e0 already contains an item with the identifier com.apple.SwiftUI.navigationSplitView.toggleSidebar. Duplicate items of this type are not allowed."
I got the same and I don't use a search bar. My app can switch between tab control or Navigation Split View and I debug by putting a toggle. Up until now I've used the NavigationSplitView for my Mac version but I saw the tabview update and decided to try it and my app crashed when pressing a tab and gave me no good warning.
Creating a simple tabview to NavigationSplitView sample code I get the same, with this mentioned error, when tapping a tabview option. Code:
struct ContentView: View {
@State var isCompact: Bool = false
var body: some View {
VStack {
if isCompact {
EntryTab()
} else {
EntrySidebar()
}
Toggle(isOn: $isCompact, label: {
Text(isCompact ? "On" : "Off")
})
.padding(.horizontal, 20)
.padding(.bottom, 20)
}
}
}
public struct tabControl: Identifiable, Hashable, Sendable {
public static func == (lhs: tabControl, rhs: tabControl) -> Bool {
lhs.id < rhs.id
}
public var id: Int // Tab Number
public var displayName: String
public init(id: Int, displayName: String) {
self.id = id
self.displayName = displayName
}
}
struct EntryTab: View {
let entryTabs = [
tabControl(id: 0, displayName: "row 0"),
tabControl(id: 1, displayName: "row 1"),
tabControl(id: 2, displayName: "row 2"),
tabControl(id: 3, displayName: "row 3")
]
@State private var selectedTab: Int = 0
var body: some View {
TabView(selection: $selectedTab) {
ForEach(entryTabs) { tabCtrl in
NavigationSplitView {
Text("Selected tab is \(selectedTab)")
} detail: {
Text("Choose item from sidebar... in future this would be content")
}
.tabItem {
Text(tabCtrl.displayName)
}
.tag(tabCtrl.id)
}
}
}
}
struct EntrySidebar: View {
@State private var selectedTabID: Int?
let entryTabs = [
tabControl(id: 0, displayName: "row 0"),
tabControl(id: 1, displayName: "row 1"),
tabControl(id: 2, displayName: "row 2"),
tabControl(id: 3, displayName: "row 3")
]
var body: some View {
NavigationSplitView(sidebar: {
List(entryTabs, id:\.id, selection: $selectedTabID) { thisItem in
Text(thisItem.displayName)
}
}, content: {
Text("Hi selected tab: \(String(describing: selectedTabID))")
}, detail: {
Text("Choose item from sidebar... in future this would be content")
})
.onAppear() {
// Set the selected tab
selectedTabID = 1
}
}
}
I had a similar issue when adding entities on a background thread. Got it working by working in a different context. So rather than passing in the entity I passed in the model container and entity persistent id and recreated in in a new context. Share more about it here: https://www.simplykyra.com/swiftdata-solving-fatal-errors-and-exc_bad_access-while-handling-entities-on-different-threads/
Have you found an answer to this? My code is currently creating SwiftData entities each on a background thread and sometimes it works (73 created) and sometimes it crashes on a random one. My thread sidebar looks the same for the crashed thread.
I hadn't thought of using it as a singleton.... may switch to that Thanks.
Is this on the website or for the app? I have a personal account with no issues on the website. That said my iPhone app was giving me that problem. I ended up deleting and reinstalling the app and it worked fine after that… so far.
Never mind. It's somehow working now :)
I'm experiencing this right now. Has anyone solved it?
I was able to solve the issue I needed the sample code for by using DispatchQueue.main.async. Found through this StackOverflow question (https://stackoverflow.com/questions/74159031/swiftui-publishing-changes-from-async-model). Keeping the question open in case this project exists and is floating around somewhere. Would love the official sample to confirm but good going forward.
In case anyone is looking into this in the future. I asked on StackOverflow (https://stackoverflow.com/questions/74159031/swiftui-publishing-changes-from-async-model) and was told to put it on the main thread with ‘@MainActor’ or use DispatchQueue.main.async. I enclosed each of my assignments with the DispatchQueue.main.async and it now works without the warnings.
For example my async method now looks like:
class myAsyncViewModel: ObservableObject {
@Published var imageName: String = "questionmark"
@Published var title: String = "title"
@Published var subTitle: String = "subtitle"
func thisMethodTakesTime() async -> String? {
print("In method: \(imageName), \(title), \(subTitle)")
DispatchQueue.main.async {
self.title = "MY METHOD"
self.subTitle = "Starting out!"
}
try? await Task.sleep(nanoseconds: 1_000_000_000)
DispatchQueue.main.async {
self.subTitle = "Between"
}
try? await Task.sleep(nanoseconds: 1_000_000_000)
DispatchQueue.main.async {
self.subTitle = "About to return. Success!"
}
print("In method: \(imageName), \(title), \(subTitle)")
return "RETURN RESULT"
}
}
In case anyone has the same issue I wanted to share how I fixed it.
The original question complained about the following error:
Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}
I ultimately fixed it by going to App > Target > Info tab and added the three Privacy - Health... Usage Description Strings. Later I realized I only needed one for my purposes. Also, before or during this process, I went to App > Target > Signing & Capabilities tab and pressed the plus in the top left corner to add HealthKit iOS. Here are the photos showing this:
The signing and capabilities needed HealthKit added (not sure if this is REQUIRED but figured I'd share.
This fixed the above error but brought me to the next one.
Error Domain=com.apple.healthkit Code=4 "Missing application-identifier entitlement" UserInfo={NSLocalizedDescription=Missing application-identifier entitlement}
I couldn't figure it out and then finally asked on StackOverflow and got the answer..
The main portion was adding the Privacy - Health Update Usage Description string to the Info tab. Originally I added all three Privacy - Health --- Usage Description but after it worked I went back and removed the ones that weren't needed.
. To fix it you need to go to your entitlements file (icon has a yellow checkmark with a squiggly circle around it) and manually add application-identifier, change the type to a String, and set the value to your Bundle Identifier (found under App > Target > Signing and Capabilities > Signing). Later I noticed the Bundle Display Name under the info tab so not sure if those two are connected.
Good luck :)
After reading it seemed people got through this removing the app from the device. This is a newly created app. Removing from the simulator does not fix error. Another comment mentioned they got their error because of Apple account. I logged in with my Apple ID and I still got the same error.
Recap: Simulator iOS doesn't work as Code=4 "Missing application-identifier entitlement while my macOS Ventura 13.0 and non-beta iPad don't work as Code=1 "Health data is unavailable on this device".
UPDATE: I figured out how to get past this error by going to App > Target > Info tab and added the three Privacy - Health... Usage Description Strings.
My issue now is I'm getting Code 1 Health data is unavailable when running on my non-beta Mac AND my beta iPad. I get error 4 when run on my simulator. Specifically Code=4 "Missing application-identifier entitlement. Is there anyway to debug/work with health data in a development setting? Or is there a way to get it working on the simulator?
I've been converting my image to Data and then storing it that way. Not sure if that's the proper way or not. Plus, since the upgrade, my normal macOS way to convert isn't working. Used to use tiffRepresentation?.bitmap?.png but now there's no bitmap on the tiffRepresentation.