I have a list of swiftdata objects I want to fetch and show in a swift UI list. My problem is the swiftdata object in my DB is not the object I directly want to use in my view. I want to map it to a different viewModel and use that which makes my life wayyy easier.
problem is every single example I see online of swift data, just uses the magic @Query operator to get their data, and then directly show it on the UI.
What's the best way for me to load the data on initialization but map it to a different type? I've tried this, but it just crashes.
init() {
do {
modelContainer = try ModelContainer(for: MY_DATA_CLASS.self)
} catch {
fatalError("Could not initialize ModelContainer")
}
let serverList = FetchDescriptor<MY_DATA_CLASS>(
sortBy: [SortDescriptor(\.displayOrder)]
)
do {
viewModels = try modelContext.fetch(serverList).map {
ViewModel(server: $0)
}
} catch {
print(" WHAT THE HECKKK: " + error.localizedDescription)
}
}
NSFetchRequest could not locate an NSEntityDescription for entity name ....
any suggestions greatly appreciated. Using @query works fine so I know the rest of the DB is setup correctly.
Post
Replies
Boosts
Views
Activity
I'm having a weird issue. I have a watch app which communicates with the phone using a WCSessionDelegate.
if the phone app is open everything works fine, but if the app is closed, when the watch sends a message, my app is woken from the background, after which onAppear() is called in my app which causes a swiftData query to run.
Calling any swiftdata function from a backgrounded app causes it to immediately crash with the following stack trace. Any ideas what im doing wrong? or a better way to trigger my code instead of onAppear, so it won't be called when my watch wakes my app from the background?
.onAppear {
reloadData()
}
private func reloadData() {
let fetch = FetchDescriptor<SavedServer>(
predicate: nil,
sortBy: [.init(\.displayOrder)]
)
guard let results = try? modelContext.fetch(fetch) else {
self.rows = []
return
}
self.rows = results
}
-------------------------------------
Translated Report (Full Report Below)
-------------------------------------
Incident Identifier: 057999AF-7840-410E-B3EE-29082C5AED00
CrashReporter Key: 28AF2AA0-4626-9964-9664-36077DAF4E1A
Hardware Model: MacBookPro18,2
Process: MC Status [68915]
Path: /Users/USER/Library/Developer/CoreSimulator/Devices/C61698BA-C4CA-4DD9-B824-DBF57AC65090/data/Containers/Bundle/Application/A685371C-9174-4CF7-9E99-D573310CC3E5/MC Status.app/MC Status
Identifier: com.shemeshapps.MinecraftServerStatus
Version: 2.0 (1)
Code Type: ARM-64 (Native)
Role: Non UI
Parent Process: launchd_sim [55432]
Coalition: com.apple.CoreSimulator.SimDevice.C61698BA-C4CA-4DD9-B824-DBF57AC65090 [164301]
Responsible Process: SimulatorTrampoline [53834]
OS Version: macOS 13.4.1 (22F82)
Release Type: User
Report Version: 104
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Triggered by Thread: 0
Last Exception Backtrace:
0 CoreFoundation 0x18046589c __exceptionPreprocess + 160
1 libobjc.A.dylib 0x18005c09c objc_exception_throw + 56
2 CoreData 0x184989b94 -[NSFetchRequest(_NSInternalMethods) _incrementInUseCounter] + 0
3 CoreData 0x1849aa99c -[NSManagedObjectContext executeRequest:error:] + 164
4 CoreData 0x1848fe250 NSManagedObjectContext.fetch<A>(_:) + 80
5 SwiftData 0x1a89b7ad8 ModelContext.fetch<A>(_:) + 124
6 SwiftData 0x1a89c48c0 dispatch thunk of ModelContext.fetch<A>(_:) + 20
7 MC Status 0x102530ef4 MainAppContentView.reloadData(forceRefresh:) + 752 (MainAppContentView.swift:112)
8 MC Status 0x102533540 closure #2 in MainAppContentView.body.getter + 44 (MainAppContentView.swift:78)
9 SwiftUI 0x108a0b7a0 0x107b8c000 + 15202208
10 SwiftUI 0x108a0b7bc 0x107b8c000 + 15202236
11 SwiftUI 0x108a0b7a0 0x107b8c000 + 15202208
12 SwiftUI 0x108fdce70 0x107b8c000 + 21302896
13 SwiftUI 0x108fd6ec0 0x107b8c000 + 21278400
14 SwiftUI 0x1081edb24 0x107b8c000 + 6691620
15 SwiftUI 0x10928d650 0x107b8c000 + 24122960
16 libdispatch.dylib 0x1801424f4 _dispatch_call_block_and_release + 24
17 libdispatch.dylib 0x180143d3c _dispatch_client_callout + 16
18 libdispatch.dylib 0x180152b24 _dispatch_main_queue_drain + 1272
19 libdispatch.dylib 0x18015261c _dispatch_main_queue_callback_4CF + 40
20 CoreFoundation 0x1803c61b4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
21 CoreFoundation 0x1803c08cc __CFRunLoopRun + 1936
22 CoreFoundation 0x1803bfd28 CFRunLoopRunSpecific + 572
23 GraphicsServices 0x189864bc0 GSEventRunModal + 160
24 UIKitCore 0x103b30208 -[UIApplication _run] + 868
25 UIKitCore 0x103b33e80 UIApplicationMain + 124
26 SwiftUI 0x108a10524 0x107b8c000 + 15222052
27 SwiftUI 0x108a103c4 0x107b8c000 + 15221700
28 SwiftUI 0x108722088 0x107b8c000 + 12148872
29 MC Status 0x102506d30 static MCStatusApp.$main() + 40
30 MC Status 0x102506de0 main + 12 (MCStatusApp.swift:12)
31 dyld_sim 0x1028fd558 start_sim + 20
32 dyld 0x1026b1f28 start + 2236
33 ??? 0x3c15800000000000 ???
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x102f1cfa8 __pthread_kill + 8
1 libsystem_pthread.dylib 0x10285712c pthread_kill + 256
2 libsystem_c.dylib 0x1801375ec abort + 104
3 libc++abi.dylib 0x180263c78 abort_message + 128
4 libc++abi.dylib 0x180255198 demangling_terminate_handler() + 300
5 libobjc.A.dylib 0x180037bf0 _objc_terminate() + 124
6 libc++abi.dylib 0x180263150 std::__terminate(void (*)()) + 12
7 libc++abi.dylib 0x180263100 std::terminate() + 52
8 libdispatch.dylib 0x180143d50 _dispatch_client_callout + 36
9 libdispatch.dylib 0x180152b24 _dispatch_main_queue_drain + 1272
10 libdispatch.dylib 0x18015261c _dispatch_main_queue_callback_4CF + 40
11 CoreFoundation 0x1803c61b4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
12 CoreFoundation 0x1803c08cc __CFRunLoopRun + 1936
13 CoreFoundation 0x1803bfd28 CFRunLoopRunSpecific + 572
14 GraphicsServices 0x189864bc0 GSEventRunModal + 160
15 UIKitCore 0x103b30208 -[UIApplication _run] + 868
16 UIKitCore 0x103b33e80 UIApplicationMain + 124
17 SwiftUI 0x108a10524 0x107b8c000 + 15222052
18 SwiftUI 0x108a103c4 0x107b8c000 + 15221700
19 SwiftUI 0x108722088 0x107b8c000 + 12148872
20 MC Status 0x102506d30 static MCStatusApp.$main() + 40
21 MC Status 0x102506de0 main + 12 (MCStatusApp.swift:12)
22 dyld_sim 0x1028fd558 start_sim + 20
23 dyld 0x1026b1f28 start + 2236
Was watching this latest WWDC 2023 video and had a question. I see about 17:20 in, they mention you can now put the shortcut provider in an app intent extension.
https://developer.apple.com/videos/play/wwdc2023/10103/
This works fine by itself and I can see all my shortcuts and use siri, but as soon as I try to call into the extension from the main app in order to trigger updateAppShortcutParameters() or any other code, I get a linker error. Am I doing something obvious wrong? Note, I called it a framework, but it Is just an extension. Cant figure out how I am supposed to be calling this method.
Any help is greatly appreciated!
https://developer.apple.com/documentation/appintents/appshortcutsprovider/updateappshortcutparameters()?changes=_4_8
https://imgur.com/a/yDygSVJ
I've watched the following 3 WWDC videos on intents and after watching, decided to try and implement an AppIntentsExtension. All my intent action does is run a network request in the background. So it seems like I should be putting it in an AppIntentsExtension instead of in the main app to avoid having to launch the app in the background, and instead have it run directly in the extension.
2022
https://developer.apple.com/videos/play/wwdc2022/10032?time=1620
2023
https://developer.apple.com/videos/play/wwdc2023/10103?time=880
2024
https://developer.apple.com/videos/play/wwdc2024/10134/?time=992
If I just put the intent and shortcut provider code I need inside an AppIntentsExtension, everything works fine except for 1 thing.
I have no way to call updateAppShortcutParameters on my AppShortcutsProvider from the main app because it is defined inside the extension. So I have no way to let siri know to re-query my parameterized phrases, and so siri doesnt expose any new phrases when things in the app are updated.
So to solve this, I somehow need a way to access the AppShortcutsProvider from within the main app to be able to call updateAppShortcutParameters
https://developer.apple.com/documentation/appintents/appshortcutsprovider/updateappshortcutparameters()
What is the recommended way to do this?
Should I be trying to create a separate framework with the shortcut provider, which is then used by both the main app an extension? I tried doing this but ran into a few build errors. I tried changing the target membership of the AppShortcutsProvider to have the file in the extension, but include both the main app and intent extension, this seems to work, but I am unsure if this is the correct approach.
I'm attempting to migrate my app's Homescreen widget from using the old SiriKit Intent system to the new AppIntent system but running into an issue.
This WWDC video is the main source of information I have been going off of.
https://developer.apple.com/videos/play/tech-talks/10168/
The problem I'm having is no matter what config I try, as soon as I upgrade to the new app with the AppIntent Widget, all the current widgets on the homescreen stop working, and just show a black screen. Deleting and re-adding the widget to the homescreen from scratch works fine though.
I found someone else with a similar issue here on the Apple developer forums, but it didn't look like there was much information or resolution.
https://developer.apple.com/forums/thread/759751
https://feedbackassistant.apple.com/feedback/14678285
I used XCode's built in Intent migration UI to create the new CustomIntentMigratedAppIntent which should help migrate the old widgets to the new system. I also double-checked all the names and variable types, and the automatically generated file seems to be correct.
I even went ahead and created a brand new empty app with the same app and extension bundle id, and made a hard coded widget which contains the migrated intents, just to make sure nothing in my widget or app code is wrong. And even with the empty app with hardcoded views, the widget is still dead after upgrading the app.
It almost seems like the widget is getting disassociated from the app.
Even though the app has a new name, the old widget will still show the old name, the widget also does not give any option to edit it, only delete it. And on the new app, I am able to attach my debugger to the widget extension process, but no code inside my timeline, intent, or entity seem to ever be called/run.
I looked at the Console log while forcing a refresh of the widget, and I've attached the full logs here, and there is a lot in there about it refreshing widgets.
SEE LOGS HERE: Tomer_Logs.txt
But one line that stands out, is this error which appears every time the old widget is refreshed
[com.shemeshapps.MinecraftServerStatus::com.shemeshapps.MinecraftServerStatus.MinecraftServerStatusHSWidget:MinecraftServerStatusHSWidget:-5734839072461827392] Reload failed; 0 retries remaining: ChronoCoreErrorDomain (1) Error Domain=ChronoCoreErrorDomain Code=1 "Unknown extension process" UserInfo={NSLocalizedDescription=Unknown extension process}
Any help in greatly appreciated, Either issues you think might be causing this, or any tips on debugging further!
Thanks!
I've gone ahead and filled a Feedback, as well as submitted a DTS issue, as this is a release blocker. (The feedback contains the new and old project files to allow anyone to reproduce.)
FB15531563
DTS case ID: 9677328
I'm converting my app to use the new AppIntent system in the widgets from the old custom intent based system. I'm trying to implement defaultResult in my EntityQuery, so the widget can be ready to go as soon as it's added to the home screen.
https://developer.apple.com/documentation/widgetkit/making-a-configurable-widget
https://developer.apple.com/documentation/appintents/uniqueappentityprovider/defaultresult()
But the weird behavior is that defaultResult seems to be called once when the first widget is added to a home screen, and shows the correct data, but then the defaultResult method is never called again when subsequent widgets are added.
It just uses the result from when the first widget was added. This especially causes issues because then a user will delete the item that the first widget was referencing, but adding new widgets still try to refer to the old AppEntity that no longer exists, even though "entities(for " returns nil for those IDs to signify that item no longer exists.
I would have assumed defaultResult would get called every time a new widget is added, but haven't seen anyone else complain about this. Has anyone seen this issue before or have any advice?
I'm adding widget interactivity to my home screen widgets via buttons and AppIntents, but running into some interesting behavior the way the timeline is reloaded after.
I'm following this guide from Apple
https://developer.apple.com/documentation/widgetkit/adding-interactivity-to-widgets-and-live-activities
And the widget is guaranteed to be reloaded when a button pressed with an intent, But whenever the AppIntent is done with the perform action, the widget timeline is always reloaded twice. It's also interesting to note that both reloads happen after the perform method. If you add a 10 second sleep in the perform, nothing happens for 10 seconds, then both reloads happen.
This issue with this is 2-fold.
calculating and rendering the entire widget timeline can be Networking and DB intensive operations, so I would ideally like to avoid doing all the work twice and save the users battery and processing.
The even worse issue, sometimes data on the server changes in between the split second duplicate widget timeline reloads, causing the widget to flash one state, then update to another a second later which is not a good user experience.
I have a sample project which shows the issue and is very easy to reproduce.
The widget simply keeps track of the number of reloads.
To reproduce:
Add the widget to the homescreen
Press the refresh button, and observe the timeline refresh count always goes up by 2.
I've filed a Feedback and attached the sample project and screen recording for anyone to reproduce.
FB15595835