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?:
Post
Replies
Boosts
Views
Activity
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.
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.
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?
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’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?
The ‘Keep your complications up-to-date‘ WWDC20 session video used a kite flying app that was a WatchKit extension app. I’m planning on building a new 100% SwiftUI watch only (independent) app for watchOS 7 using Xcode 12 Beta and I’m wondering how I do complication updates in that case without the watch kit extension APIs used in the video session.
I’m currently working on a watch only app and I’m trying to understand how best to persist my model data for the user’s data. I plan to release for watchOS 7. I plan to use SwiftUI. I’m still trying to decide if I continue using a WatchKit extension project structure (created from Xcode 11) or start again with a 100% SwiftUI watch only project using Xcode 12. Does that affect selection of a data persistance approach?
I’ve used CloudKit for data persistance in my iOS app. I guess I could do this again For my watchOS app, or maybe in conjunction with Core Data, but how do I assess the various options? What are the best practices in choosing an approach to persist model data for a watch app? My model data won’t be too complex. Just an array of items, each item consisting of a string and image.
Any recommendations are welcome.
I viewed the App Essentials in SwiftUI video this morning. I’m still having trouble understanding when and why I would use the @StateObject property wrapper. I reviewed the developer documentation for the @StateObject property wrapper but it is still confusing on why and how I might use that in a new SwiftUI App struct versus just using a model struct that conforms to the ObservableObject protocol and using the @Punlished property wrapper for a model object and an @ObservedObject property wrapper for an instance of the model in the ContentView.
I know that Apple provided WWDC20 jacket and pins to the Swift Student Challenge winners. However, I really hope Apple will find a way for developers to purchase a WWDC20 jacket and pin set during/after the conference.
For myself, like many others I assume, this is my first ever WWDC. Now, not being able to attend in person, I’d hate to miss out on the WWDC experience of getting the jacket and pins so iconic to each WWDC.
It would also be nice if Apple was able to make available special Infinity Loop and Apple Park Apple Store T-shirts and merchandise since none of us could get there physically.
Have a great conference everyone. I know I am enjoying it so far.
Earlier this year I started a new watch only project in Xcode 11. I’m not too far along and I’m interested in recommendations on if I should just begin the project again using Xcode 12 beta and the new App structure using the new app and scene protocol APIs and the new app.swift file template. Would I be gaining a lot with using this new template for organization and underlying SwiftUI capability versus continuing on with the watch only watchKit extension project structure that I’ve got currently under Xcode 11?
I’ve watched the new What’s New in SwiftUI session and I’m trying to understand the additional benefit I’d get by starting with this new project template from Xcode 12 beta for a Watch only app.
I’m not really planning on releasing this watch only app prior to release of watchOS 7 and the formal build of Xcode 12 so I’m not too worried about getting it out for watchOS 6 in the near term.
Will App Structure and Behavior in SwiftUI be discussed in a session? I’m hoping to understand more about Swift 5.3 App and Scene protocols and the @main attribute and how I might, or might not, use it in my SwiftUI app I’m starting to work on.
I’m currently working on a watchOS app in the early stages using Xcode 11.2 running on macOS Catalina. I’d like to do any further development work on the app using Xcode 12 beta, which I’ve downloaded and I’m running on macOS Catalina. Does anyone know whether or not I also need to download macOS Big Sur as well as Xcode 11 beta (running in Big Sur) in order to get all the features of Xcode 12 for my on-going development?
I have a detailed view controller. It is made up of dynamic, model based, text label subviews that are placed by code on the view within a scrollview with set spacing. I have anchored the last text label (lastLabel) to the scrollview bottom anchor with the following:
scrollView.bottomAnchor.constraint(equalTo: lastLabel.bottomAnchor, constant: 0).isActive = true
The above line was added on the suggestion from a DTS ticket, and it solved some other label subview and scroll view issues.
However, the problem I'm having, on all device sizes by the way, is that if I start in portrait orientation, my lastLabel is displayed as expected with correct spacing to the previous (above) text label, but then when I rotate to landscape orientation, then rotate back to portrait orientation, the lastLabel is shifted down and spacing to the previous (above) label is increased.
If I navigate back to the parent view, then return again to the detail view in portrait orientation, the lastLabel is correctly positioned again relative to the previous (above) label.
I don't seem to be able to figure out how to fix this bug so that the lastLabel on the view always retains the proper spacing to the previous (above) label, even after transitioning from landscape to portrait orientation.
I do not see this problem when transitioning from portrait to landscape. The landscape orientation always displays the lastLabel in the correct relative position to the previous (above) label.