Post

Replies

Boosts

Views

Activity

Recommendations/Advice on Core Data template entity approach
Prerequisites I have three entities: Record, Header, and Row. Record: id index date children (one-to-many relationship with Header entities) Header: id index title parent (Record) (Is null if this Header is a subheader) headerParent (Header) (Is null if this Header is not a subheader) subheaders (one-to-many relationship with Header entities) children (one-to-many relationship with Row entities) Row: id index title value parent (Header) Issue I need to have default data in these entities. When creating a new Record, I want it to copy all default entity data (except for the Row's value) to generate a new Record ready for new notes. Additionally, if the order (index) of the entities' data or their titles are updated, I want the default data to update automatically so the user doesn't have to make these changes manually every time. Proposed Solution My approach is to add a field called isBaseEntity to each entity. When isBaseEntity is true, this entity contains the default information. I would then update this base entity with any changes made to the non-base ones. Additionally, I would exclude this base entity when fetching the rest of the data, as it serves as the default template. Question Is this a good and efficient approach? Do you have any other suggestions or recommendations for improving this? I would really appreciate any help you can provide.
0
0
227
Jul ’24
Prevent Data Retention in Core Data When View is Dismissed
Prerequisite Information I am trying to use Core Data in my SwiftUI app. I have created 3 entities, a Record, a Header and an Entry. The Record contains the Header entities. The Header contain other Header or Entry entities. The Entry has a title and a value. My List view looks like this, I fetch the records with a @FetchRequest. NavigationView { List { ForEach(records) { record in NavigationLink(destination: DetailView(headers: record.headers!.allObjects as? [Header] ?? [], isNewEntry: true)) { Text(record.id!.uuidString) } } } } In my DetailView I have a form that passes the details to other views which create a DisclosureGroup for a Header, a bold Text for a subheader, and a TextField for an Entry let headers: [Header] let isNewEntry: Bool @Environment(\.managedObjectContext) var managedObjectContext init(headers: [Header], isNewEntry: Bool = false) { self.headers = headers self.isNewEntry = isNewEntry } Form { ForEach(Array(headers.enumerated()), id: \.element) { index, header in HeaderView(header: header, isExpanded: Binding( get: { self.expandedStates[header.id!, default: false] }, set: { self.expandedStates[header.id!] = $0 } )) } } Problem When I press the record in the list, it takes me to the DetailView where I can write something in the TextField. Then if I slide back and do not press the save button and then press the same record again from the list, it retains the data I wrote in the TextField. Question How can I make it so that the data is not retained if I slide back except only when I press the save button? Please let me know if any other information is needed or if something is unclear.
0
1
387
Jun ’24
Update a view from a button in another view.
Prerequisite Information Hello, I have a macOS app which is split between two views each having it's own rows/cells . You can click a row in the first view and it opens a view with more rows in the second view. I have a custom view struct, which is a button saving and receiving data from Core Data, that depending on which of the two views you call it, it has a stage, and changes colour. The relationship between them is, a row from the first view, has multiple rows from the second view. (This applies for both the views and the Core Data entities) What should be happening: First view button Normally, when you press the button on the first view, it should update the Core Data value and change it's colour. Then it should update all of the stage values of all it's rows in the second view in Core Data, and then refresh the second view so their buttons reflect the changes in Core Data. Second view button When you press the button in the second view, it should update it's stage value in Core Data, change it's colour, and then update the value of it's parent view in Core Data and refresh to update it's colour. What is happening now: What is happening now, is that it correctly updates the stages in Core Data, and also applies the correct colours on the buttons when the app initializes, but when you press the button on either the first or the second view, it only changes the colour of the button pressed. It does not refresh the other view, so it reflects the value in Core Data. I tried adding a binding variable that i would change when the button was clicked, without results. I also tried manually changing the values in the view model, but again no success. How could I make this work?
13
0
2.7k
May ’21
How can I create a multiline/wrap-able HStack/VStack?
Hello, I am trying to create an HStack with an array of my custom view that has images and information taken from my database model, that when it reaches the end of the window, it takes the rest of the cells (my custom views) below to the next line. Then if you resize the window, or change the size of the cells (through a slider) it should reorder the cells so they fit the window without needing horizontal scrolling. I have made numerous attempts to try to find a way to do this, but I couldn't find any way to make it work correctly. I am developing a macOS app in macOS 10.15, so I only have SwiftUI 1 available. Could someone help me?
3
0
6.9k
Nov ’20
How can I have a variable that stores a few different struct types?
Prerequisite Information Hello, I would like to know if there is some way to create a variable, that could store a few different struct types. Example: struct TestOne { var id: Int var name: String var colour: String static var text: String = "TestOne" } struct TestTwo { var id: Int var lesson: String var description: String static var text: String = "TestTwo" } struct ChosenTest { var test: testOne or testTwo func printText() { print(test.text) } } I tried adding a protocol, protocol TestGroup {} and then make the structs adhere to it and change it in the ChosenTest, like struct TestOne: TestGroup { var id: Int var name: String var colour: String static var text: String = "TestOne" } struct TestTwo: TestGroup { var id: Int var lesson: String var description: String static var text: String = "TestTwo" } struct ChosenTest { var test: TestGroup func printText() { print(test.text) } } When i try to create a ChosenTest struct and initialize the test variable, it fails because it wants the object of that type and not the actual TestOne.Type or TestTwo.Type Questions How could I make the TestOne.Type/TestTwo.Type conform to the ChosenTest protocol? How could i do something like the code below (or achieve the same result), if the question above is not possible to be done? struct ChosenTest { var test: TestGroup func printText() { print(test.text) } }
12
0
1.9k
Dec ’20
How could I develop and deliver an offline library app with millions of images?
Prerequisite Information Hello, I am creating a macOS app that loads lots of information from an SQLite database to a table view. The database has about a million of records in the table and each record has a field with the local path of an image. The size of the database file is about 1 GB. The size of the folder containing those images is about 17 GB. Questions What is the best and most efficient way possible to be able to create, bundle and distribute this app in the app store or any other place (preferably without having to download 18 GB) ? I know that it would be a lot better by having it on a server and downloading it, but I want to make the app completely offline.
3
0
518
Dec ’20
Deselect list row in SwiftUI
Hello, I am creating a macOS app with SwiftUI and I added a sidebar with NavigationView and inside I added a List with NavigationLinks. I also added a separate NavigationLink at the bottom of the Window. The behavior I want to have is, when I press the NavigationLink at the bottom, then to deselect all the other NavigationLinks in the sidebar List. Most probably I still haven't properly understood SwiftUI and it's declarative paradigm yet, and I can't see a way to do this. Could someone help me? This is the code I have written: Enum enum Menu: String, CaseIterable {     case library = "Library"     case catalogue = "Catalogue"     case filter = "Filter"   } Main Code struct SplitView: View {     var body: some View {         NavigationView {             VStack(alignment: .leading, spacing: nil) {                 MenuList()                 Divider()                 Spacer()                 NavigationLink(destination: FilterSpace()) {                     HStack {                         Text("􀌈")                         Text("Filter")                     }                         .padding(5.0)                         .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.gray, lineWidth: 1))                 }                 .cornerRadius(10)                 .padding([.leading, .trailing], 7.0)                 .buttonStyle(BorderlessButtonStyle())                 Spacer()             }         } .navigationViewStyle(DoubleColumnNavigationViewStyle()) .frame(minWidth: 800, maxWidth: .infinity, minHeight: 500, maxHeight: .infinity)     } } Sidebar List struct MenuList: View {     @State private var selection: Menu? = .library     var body: some View {         List(selection: $selection) {             Section(header: Text("Main Menu")) {                 NavigationLink(destination: LibrarySpace()) {                     Text(Menu.library.rawValue)                         .font(.subheadline)                         .padding(5)                 }                 NavigationLink(destination: CatalogueSpace()) {                     Text(Menu.catalogue.rawValue)                         .font(.subheadline)                         .padding(5)                 }             }         }         .listStyle(SidebarListStyle())         .frame(minWidth: 150, maxWidth: 150)         .onAppear {                 self.selection = .catalogue }     } }
4
0
5.8k
Oct ’20
Use SwiftUI for iOS 13+ with some fallback mechanism for iOS 12 and lower.
Hello, I want to use SwiftUI, but I want to also support iOS 12 and maybe iOS 11, as - according to Apple - there is around 19% of total users, who are still not on iOS 13 (Some of which are unable to, due to older devices). I already know how to use UIKit although I prefer not using storyboard and writing it in code, but using UIKit with pure code, sometimes is a bit problematic as it takes too much time to figure out how to do very simple things (at least in my experience :p) Is there any good/proper (or kind of official) way to use SwiftUI for devices with iOS 13+ but also support devices with iOS 12 and iOS 11 through some other mechanism? (I am guessing that I will probably need to write the UI two times, one with SwiftUI and one with UIKit) If not, should I create two projects, one for the future versions using SwiftUI and one for the older ones? What do you recommend? Thanks in Advance
3
1
6.0k
Jun ’20