I’m practicing with creating a multiplatform App (could just as well be an iOS app) using the boilerplate code generated by Xcode when Core Data and CloudKit check boxes are selected upon project creation. Xcode generates a persistence.swift file with the Core Data stack code and uses the @FetchRequest property wrapper in the ContentView.swift file. However, I’d like to move some of the more “view model” or non-UI boilerplate code out of ContentView and into a more MVVM-like view model class file so that I keep the ContentView focused on my UI. I would like to continue using the provided persistence.swift file as my Core Data manager content.
Can I still use the power of @FetchRequest in the ContentView (I think it has to remain in my content view), while moving the other boilerplate code Xcode generated out of ContentView to a new view model class file? For example, I’m talking about the automatically generated code for addItem() and deleteItem().
In other words, can I use both the @FetchRequest approach for accessing my entity model while also making my code fit a more MVVM architecture?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Can I store AR objects/assets in Core Data? How do I load Core Data model entity/attributes with the AR data? How to retrieve for display in a SwiftUI view?
I want to be able to have my iOS app use SF symbols and store them in my Entity attribute in my Core Data model. I’m guessing I need to use the binary data attribute type because SF symbols are displayed using an Image view. I’m looking for a way to take an SF symbol from a SwiftUI view and store it in the Core Data Model. I also want to be able to retrieve that stored entity attribute from my Core Data model and display it in any SwiftUI views I might choose.
How can I do this?
Wondering if I could get benefits from using Xcode Cloud if I'm just a solo, hobbyist Developer? I don't need to collaborate with others on my code, but maybe the built in workflow and tie in with App Store Connect and TestFlight might be worth looking into. I'd appreciate any thoughts from the Apple team.
How can I update/save a Property Change for Child Item from a Hierarchical List View of Items
See the following app screens:
Content View Screen:
Content View with hierarchical list children rows disclosed:
Parent Row Detail View:
Child Row Detail View:
Referencing the above views, here are the steps I do and the resulting problem I’m trying to solve:
Launch the app.
From the Functions (Content View) presented at launch, see that there is one item listed in a list view (1.0 Move Vessel)
Click the yellow (my app accent color) disclosure arrow at the right of the list item.
Two subordinate child list rows appear under the parent list item, 1.1 Move Position and 1.2 Hold Position.
When I tap the parent item (1.0 Move Vessel) in the hierarchy list, I'm successfully able to navigate to a detail view for that tapped item.
Edit the description of the 1.0 Move Vessel item (defaults to test) of the tapped item properties in the detail view using a TextEditor view.
Click yellow Save button at top left of detail view. The app navigates back to the parent Functions (Content View).
Click on the parent 1.0 Move Vessel row again.
See that description was successfully saved and now displayed from the change made in Step 5 and 6.
Repeat steps 5 through 8 again for 1.1 Move Position list row.
See that the edit/change made to the description was not saved and the default test1 description is displayed instead (not what is wanted).
Repeat steps 5 through 8 again for 1.2 Hold Position list row.
See that the edit/change made to the description was not saved and the default test2 description is displayed instead (not what is wanted).
I think I may have a problem in my save code logic and I'm trying to investigate.
Here are the swift files for the Detail View, the View Model, and the Model (I’ve not included the content view code because that code is working ok with the detail view. Again, I think the problem is in my save button and function call code for updating the view model. NOTE: sorry that I can’t seem to figure out how to get all the code for a file contiguous in the code view. I seem to have some closing braces that don’t appear in the code view. I think you can still follow the code.
FunctionDetailView.swift
struct FunctionDetailView: View {
@State var vesselFunction: VesselFunction
@State var vesselFunctionDescription: String
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var functionViewModel : FunctionViewModel
var body: some View {
NavigationView {
Form {
Text("Enter description below")
TextEditor(text: $vesselFunctionDescription)
.frame(height: 200)
.toolbar {
Button {
//print(vesselFunction)
vesselFunction.funcDescription = vesselFunctionDescription
//print(vesselFunction)
functionViewModel.updateVesselFunction(vesselFunction: vesselFunction)
//print(vesselFunction)
presentationMode.wrappedValue.dismiss()
} label: {
Text("Save")
}
}
}
.padding()
.navigationTitle(vesselFunction.name)
.navigationBarTitleDisplayMode(.inline)
}
}
}
struct FunctionDetailView_Previews: PreviewProvider {
static var previews: some View {
FunctionDetailView(vesselFunction: VesselFunction(id: UUID(), name: "x.x Verb Noun", funcDescription: "Description", children: nil), vesselFunctionDescription: "placeholder")
.environmentObject(FunctionViewModel())
.preferredColorScheme(.dark)
}
}
FunctionViewModel.swift
@MainActor class FunctionViewModel: ObservableObject {
@Published private(set) var decomp : [VesselFunction] = [
VesselFunction(id: UUID(), name: "1.0 Move Vessel", funcDescription: "test", children: [
VesselFunction(id: UUID(), name: "1.1 Move Position", funcDescription: "test1", children: nil),
VesselFunction(id: UUID(), name: "1.2 Hold Position", funcDescription: "test2", children: nil)
])
]
func updateVesselFunction(vesselFunction: VesselFunction) {
//
// if let index = decomp.firstIndex(where: { //(existingVesselFunction) -> Bool in
// return existingVesselFunction.id == vesselFunction.id
// }) {
//run this code
// }
// cleaner version of above
if let index = decomp.firstIndex(where: { $0.id == vesselFunction.id }) {
decomp[index] = vesselFunction.updateCompletion()
}
// else {
// for item in decomp {
// if item.children != nil {
// if let index = item.children?.firstIndex(where: { $0.id == vesselFunction.id }) {
// item.children![index] = vesselFunction.updateCompletion()
// }
// }
// }
// }
}
}
FunctionModel.swift
struct VesselFunction: Identifiable {
let id : UUID
let name : String
var funcDescription : String
var children : [VesselFunction]?
init(id: UUID, name: String, funcDescription: String, children: [VesselFunction]?) {
self.id = id
self.name = name
self.funcDescription = funcDescription
self.children = children
}
func updateCompletion() -> VesselFunction {
return VesselFunction(id: id, name: name, funcDescription: funcDescription, children: children)
}
}
As you can see from the else and for-in loop code commented out at the bottom of the FunctionViewModel code, I was trying to see if I needed to do something like this code to access the children VesselFunction array entries of the decomp published property. With the if let index code that is not commented out, the save function works but only for the top-level decomp array VesselFunction elements, not the nested children arrays elements.
Any help would be appreciated so all decomp array elements, both parent and nested children, can be updated when the TextEditor field is changed and the Save button is pressed in the FunctionDetailView. NOTE: I am only showing a 1 level deep nested array of children for the decomp property. I actually want to have multiple (at least 3) level of children arrays, so if you have any ideas how to make an updateVesselFunction function work for multiple children array elements, I would appreciate it.
I want to add Core Data/CloudKit data persistence to my app. Currently my app only uses local on device @AppStorage. Looking for best approach to bring in @FetchRequest functionality for my views, and tie my model data into Core Data and CloudKit.
In my app currently (without Core Data) my content view has an instance of my view model (separate file) accessed using an Environment Object. I use local @AppStorage persistence. See below for my Current app architecture:
In my lab this week, i got suggestions that I can modify my app structure to incorporate Core Data + CloudKit. Would the following structure work?:
I don’t have a fundamental background in Computer Science or Computing Engineering. I want to learn more about how to do code debugging, but within the bounds of how Xcode provides various debugging panes. I’ve seen a number of WWDC session videos about Xcode and debugging. However I really need to learn more about the fundamentals of using Xcode panes like the stack trace pane, the variables pane, and the debugging console. I would greatly appreciate any advice on tutorials, videos, WWDC sessions, etc. that go to the basic levels of what these panes show, how to interpret the content, and how to interact with the information in various Xcode debugging-related panes.
Thank you.
Topic:
Developer Tools & Services
SubTopic:
Xcode
Tags:
Xcode Sanitizers and Runtime Issues
Xcode
Debugging
LLDB
What is the Amend toggle used for in the new Xcode 15 beta, Source Control Navigator, Changes view?
Since NavigationView is now deprecated, I’m trying to update my code to use NavigationStack instead. However, I found that when using NavigationStack, section headers within an overall List view no longer have the collapsing triangle functionality. This used to work for NavigationView, List view, Section headers. Am I missing something? Is there a way to get back the section header collapse triangle within an overall List view when encapsulating within a NavigationStack?
I'm taking my iOS/iPadOS app and converting it so it runs on visionOS. I’m trying to compile my app, build it, for both visionOS and iOS. When I try to build for an iPhone and iPad simulator, I get the following error:
 Building for 'iphonesimulator', but realitytool only supports [xros, xrsimulator]
I’m thinking I might need to do a # if conditional compilation statement for visionOS so iOS doesn’t try to build lines of code but I can’t for this particular error find out for which file or code I need to do the conditional compilation. Anyone know how to get rid of this error? 
Topic:
Spatial Computing
SubTopic:
Reality Composer Pro
Tags:
Vision
Reality Composer Pro
visionOS
iPad and iOS apps on visionOS
When I do an archive build of my app against visionOS, the archive listing in the Organizer show the archive entry as iOS. So I have one archive build for iOS with a certain build number and another archive build for visionOS with another build number (incremented by 1) They are both listed in The Organizer but they both have iOS info. I’ve submitted them both separately to TestFlight and I’m about to try to test it on visionOS. Shouldn’t the visionOS archive build that I submit to TestFlight indicate something about visionOS instead of iOS?
Topic:
App Store Distribution & Marketing
SubTopic:
TestFlight
Tags:
iOS
Xcode
Organizer Window
visionOS
I have a multiplatform app with a single target. I’ve been focused on making release updates for just visionOS where the new version doesn’t really have any changes to the iOS/iPadOS content. When I build and archive a new version and build number for my single app target, is it an ok practice to only release the new version and build to visionOS and not iOS? The reason I ask, is that the next time I get around to releasing a version and build that has any iOS changes that I want to release on the iOS App Store, it will have skipped one or more version numbers (due to only visionOS releases) from the last time that version was seen on the iOS App Store because I was only doing visionOS releases and versions prior to that.
Topic:
App Store Distribution & Marketing
SubTopic:
General
Tags:
App Store Connect
Xcode
Organizer Window
If I have windows that occupy the shared space and are located in various spatial locations for the user, say various rooms in a home, how do I have those windows reappear/open in the same spatial location when the user returns to the shared space after being in a full immersive space? I assume it has to do with somehow setting the state of the windows before heading into the immersive view, but I’m not sure how to begin thinking about this or the type of code I would need for that. Any help for this question would be appreciated.
DESCRIPTION OF PROBLEM
I have an Apple Vision Pro App Store app called Starship SE Corps. I'm trying to add an animation for my app so that the starship entity orbits the Earth entity. I'm trying to use OrbitAnimation as discussed in the WWDC23 session "Build Spatial Experiences with RealityKit" (https://developer.apple.com/wwdc23/10080).
However, I can't get the animation to work.
STEPS TO REPRODUCE
I created a sample test app called "SampleOrbitAnimationApp" to focus in on the code I'm having trouble with.
When I build and run my sample test app, the app runs on both the visionOS 1.2 simulator and on my real Apple Vision Pro device running visionOS 1.2. However, my starship entity is static and is not animating/orbiting around my Earth entity.
I tried putting my OrbitAnimation code in the RealityView update: closure. Doing that, however causes some property scope errors because the entity I refer to in the OrbitAnimation code is my entity that I create in the RealityView code block...so the update: closure code block can't see the entity property.
Trying to make the entity reference more global at the top of the ImmersiveView (so update: closure sees the entity property) causes other parameter issues in the .app file call to the ImmersiveView and in the #Preview call to the ImmersiveView. Maybe that should be expected and I would need to workaround that (but I couldn't find a sensible way to do so). If this is the right approach, I need help on how to resolve this across the project files.
I did find some example code online where a developer put the OrbitAnimation code directly in the RealityView code block without having an update: or attachments: closure at all. I tried that approach but also couldn't get that to work.
The test sample app tries to target the OrbitAnimation and ImmersiveView code I'm struggling with (i.e. I can't get the starship to move and orbit around the Earth). It uses my same production app Package for Starship and Earth entities, built in Reality Composer Pro. Those entities, included in my sample test app, work fine on my latest production App Store release, so I think they are fine. The issue is how to do the OrbitAnimation code for those entities. I realize new capabilities are coming in visionOS 2, but I would like to make OrbitAnimation work now in my visionOS 1.2 app.
I can‘t Figure Out How to Get My Earth Entity to Rotate on its Axis. This is a follow up post from a previous Apple Developer forum post.
How would I have the earth (parent) entity rotate CCW underneath the orbiting starship child?
I tried adding the following code block to the RealityView but it is not working:
if let rotatingEarth = starshipEntity.findEntity(named: "Earth") {
rotatingEarth.transform.rotation = simd_quatf.init(angle: 360, axis: SIMD3(x: 0, y: 1, z: 0))
if let animation = try? AnimationResource.generate(with: rotatingEarth as! AnimationDefinition) {
rotatingEarth.playAnimation(animation)
}
}
Any advice on getting the earth to rotate?
I tried reviewing the Hello World WWDC23 project code, but I was unable to understand the complexity and how that sample project got the earth to rotate.
i want to do this for visionOS 1.2. I realize there are some new animation and possible other capabilities coming up in vision 2.0 but I want to try to address this issue in the current released visionOS version.
Topic:
Spatial Computing
SubTopic:
General
Tags:
Vision
Reality Composer
RealityKit
Reality Composer Pro