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.