Of course - please see my second post.
Post
Replies
Boosts
Views
Activity
Of Course:
import Foundation
import CoreData
@objc(AccountBackEnd)
public class AccountBackEnd: NSManagedObject {
}
And...
import Foundation
import CoreData
extension AccountBackEnd {
@nonobjc public class func fetchRequest() -> NSFetchRequest<AccountBackEnd> {
return NSFetchRequest<AccountBackEnd>(entityName: "AccountBackEnd")
}
@NSManaged public var accountAddress: String?
@NSManaged public var accountCity: String?
@NSManaged public var accountCompanyName: String?
@NSManaged public var accountCustomerType: String?
@NSManaged public var accountEmailAddress: String?
@NSManaged public var accountID: UUID?
@NSManaged public var accountLastDateVisited: String?
@NSManaged public var accountMarketType: String?
@NSManaged public var accountPhoneOne: String?
@NSManaged public var accountPhoneTwo: String?
@NSManaged public var accountPostalCode: String?
@NSManaged public var accountProvince: String?
@NSManaged public var accountManager: String?
@NSManaged public var accountWebsite: String?
@NSManaged public var toContacts: NSSet?
@NSManaged public var toQuotes: NSSet?
@NSManaged public var toNotes: NSSet?
}
// MARK: Generated accessors for toContacts
extension AccountBackEnd {
@objc(addToContactsObject:)
@NSManaged public func addToToContacts(_ value: ContactBackEnd)
@objc(removeToContactsObject:)
@NSManaged public func removeFromToContacts(_ value: ContactBackEnd)
@objc(addToContacts:)
@NSManaged public func addToToContacts(_ values: NSSet)
@objc(removeToContacts:)
@NSManaged public func removeFromToContacts(_ values: NSSet)
}
// MARK: Generated accessors for toQuotes
extension AccountBackEnd {
@objc(addToQuotesObject:)
@NSManaged public func addToToQuotes(_ value: QuotesBackEnd)
@objc(removeToQuotesObject:)
@NSManaged public func removeFromToQuotes(_ value: QuotesBackEnd)
@objc(addToQuotes:)
@NSManaged public func addToToQuotes(_ values: NSSet)
@objc(removeToQuotes:)
@NSManaged public func removeFromToQuotes(_ values: NSSet)
}
// MARK: Generated accessors for toNotes
extension AccountBackEnd {
@objc(addToNotesObject:)
@NSManaged public func addToToNotes(_ value: NoteBackEnd)
@objc(removeToNotesObject:)
@NSManaged public func removeFromToNotes(_ value: NoteBackEnd)
@objc(addToNotes:)
@NSManaged public func addToToNotes(_ values: NSSet)
@objc(removeToNotes:)
@NSManaged public func removeFromToNotes(_ values: NSSet)
}
extension AccountBackEnd : Identifiable {
}
I see what you mean, but from what I can tell, this its the same as what I'm calling. Pics attached.
As well, here is the section that I think is having the issue.
You won't believe this. So its 5am where I'm at and I can't sleep. I wake up to avoid keeping my partner awake, I check this, and OH MY GODD!! It works !!!!
Thank you!!!
I'm always amazed at the kindness of Internet strangers.
Cheers! And thank you again.
Hey - thanks for the reply!
Really sorry about the struct header and the WWDC issue. I'll work to correct those when I need to write again.
In the mean time I'll try to correct my code here.
Cheers!
Pardon me. I'll add those below:
PublishedClassAccount:
import SwiftUI
class PublishedClassAccount: ObservableObject {
@Published var accountNameForCoreData = ""
}
AccountBackEnd (Is my entity for Core data)
import Foundation
import CoreData
@objc(AccountBackEnd)
public class AccountBackEnd: NSManagedObject {
}
.... am I doing any of this right? I feel so deflated.
Hi @OOPer, and thank you for writing back.
I have a class file for instantiating PublishedClassAccount. As far as I can tell, it's doing the trick, but I might be wrong here. At any rate, I'll put it below for review.
I do remember you saying that they wouldn't work but I'm lost as to how I would create an object in the @Environment call anyway. I'll put that code below first:
@Environment(\.presentationMode) var presentationMode
As well, here's my code for PublishedClassAccount:
import SwiftUI
class PublishedClassAccount: ObservableObject {
//Account ID & Name
@Published var accountID = UUID()
@Published var accountCompanyNamePublished = ""
//Details
@Published var accountCustomerTypePublished = ""
@Published var accountLastDateVisitedPublished = ""
@Published var accountManagerPublished = ""
@Published var accountMarketTypePublished = ""
//Address
@Published var accountAddressPublished = ""
@Published var accountCityPublished = ""
@Published var accountPostalCodePublished = ""
@Published var accountProvincePublished = ""
//Contact Type
@Published var accountWebsitePublished = ""
@Published var accountEmailAddressPublished = ""
@Published var accountPhoneOnePublished = ""
@Published var accountPhoneTwoPublished = ""
// @Published var toContacts: NSSet?
// @Published var toQuotes: NSSet?
// @Published var toNotes: NSSet?
}
Hey cool. Thanks for taking the time to write back.
I get it. I'm a frustrating person to help out. I'm new to all of this, and trying really hard to ask for help, which is hard because sometimes the verbage is lost on me. For example instantiate vs initialize. As another example, last year I didn't understand the concept of scope, but knew there was something going on with my code, where some parts of it weren't being picked up by other parts of my code. Trying to describe in writing what I was working through, and struggling with, to an internet forum is not easy when you don't even know the words to use. Added to this, trying to make sense of Apple's documentation is often times a struggle. And I can't tell you how many countless hours I've spent googling for some form of help while I keep getting nowhere. So that's why I'm here. And in spite what I've just said, I do appreciate your help, and the countless others who have guided me over this last year and a half. It has not been easy.
So say for example, when you say instantiate, you clearly don't mean where the object is first established. I think that's initialization. So I'm left thinking you're talking about when it's called into a view. If that's the case, it's here: " @EnvironmentObject var publishedClassContact: PublishedClassContact. That's in my AddAccountView. If that's not correct, let me know.
That's called in my AddAccountView file, because I think I need that in order to run my function to save account entries. Yes it's a class. If I try and refactor I clearly cannot use @EnvironmentObject, so my only option is to use @Published, which then calls for that line of code to be initialized. I've asked for help here before about if the way I'm initializing is proper, and no one seems to be able to tell me. Again, I've struggled with finding out online how to make sense of this.
Here's what I'm trying to do, and maybe this will help shed some light on this. If you've made it this far, thank you very kindly for taking your time here:
I'm trying to use SwiftUI, Core Data, and property wrappers to create an account list. That list has several attributes per entity. And some of those entities need to connect to other entities. I have not had to wrestle with that yet. I will in time though. I'm sure.
My thought is to refactor the logic away from my views, and into classes. The 'PublishedClass' calls are for the entries of data into the account list. For example, some of them are accountAddress, accountPhoneNumber, accountCustomerType etc. That list has those already set as @Published. So again Im trying to have the publishedClass files be separate from the refactor files. Please let me know if this needs clarification. When I use property wrappers, I seem to run into issues, and I think those issues come from my lack of understanding around initializing.
Please let me know what parts of this need clarification.
Thank you again.
Ok that helps! Thank you!
I ended up re-writing my refactor file to use functions in place of property wrappers. After reading your reply and reading more about Observable Objects, I finally got passed this error:
Thread 1: Fatal error: No ObservableObject of type RefactoringAccount found. A View.environmentObject(_:) for RefactoringAccount may be missing as an ancestor of this view.
Turns out I neglected to add ".environmentObject(refactorAccount)" in the first file that loads. I can't remember what it's called, but it's got @main near its beginning. Also, and you'll be happy to know this, my refactor file now only has @Published calls in it. ;) Woot!
And now I'm dealing with getting data to store! LOL! The pain never ends.
Thanks again!
I should have added, AccountBackEnd is the name of my core data entity.
thanks!
The view that displays my list:
import SwiftUI
import CoreData
struct DatabaseViewMain: View {
@ObservedObject var refactoringAccount = RefactoringAccount.refactoringAccountSingleton
var body: some View {
VStack {
//Creates the 'add account' button
HStack {
Spacer()
NavigationLink {
AddAccountContainer()
} label: {
SubHeaderViewIcon(subheaderIcon: "plus.square", subheaderIconColor: Color("EditButtonBlue"))
}
.padding()
}
//Creates the List view
List {
ForEach(refactoringAccount.savedAccounts) {account in
NavigationLink {
AccountDetailMain(accountBackEnd: account)
} label: {
DatabaseViewCell(
accountNameFromCell: account.accountCompanyName,
accountLastVisitedFromCell: account.accountCity,
accountTotalFromCell: account.accountLastDateVisited)
//Text(account.accountCompanyName)
}
.foregroundColor(.gray)
}
.onDelete(perform: refactoringAccount.deleteAccount)
.onAppear(perform: refactoringAccount.fetchAccount)
}
.listStyle(PlainListStyle())
}
.navigationBarHidden(true)
}
}
The view that my mentor created:
import SwiftUI
import Combine
import CoreData
struct AddAccountContainer: View {
@State var account: AccountBackEnd?
func newAccount() -> AccountBackEnd {
let context = RefactoringAccount.refactoringAccountSingleton.container.viewContext
let newAccount = AccountBackEnd(context: context)
return newAccount
}
var body: some View {
VStack() {
if let account = self.account {
AddAccountView(accountBackEnd: account)
}
}
.onAppear {
if account == nil { account = newAccount() }
}
}
}
The View that I’d like to use:
import SwiftUI
import CoreData
//View for user to add a new account with
struct AddAccountView: View {
@ObservedObject var accountBackEnd: AccountBackEnd
@Environment(\.presentationMode) var presentationMode
var body: some View {
ScrollView {
VStack {
Group {
MainAddHeaderView(mainTextField: "Account", iconField: "building")
Spacer()
.frame(height: 5)
HStack {
NavigationLink {
CancelButton()
} label: {
MainHeaderViewBottomLeft()
}
Spacer()
}
}
Button(action: {
do {
try accountBackEnd.managedObjectContext?.save()
} catch {
print("There was a problem saving: \(error)")
}
presentationMode.wrappedValue.dismiss()
print("It exited")
})
{
LargeSaveButtonBlue(saveButtonLabel: "Save Changes")
}
OneHundredPointSpacer()
//Exit
LargeDeleteButton(deleteButtonLabel: "Delete Changes")
OneHundredPointSpacer()
}
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)
}
}
}
Same here, everyone. This is a new frustration for me so hoping it gets resolved soon.
@SteveRoberson, do you mind sharing a bit about how you made this response work? I'm trying to make this work for something I'm working through but running into issues. I think there's a bit of mystery for me in terms of what your property's were and how they related to this response?
So what I'm trying to do is use a FetchRequest to handle a one-to-many object fro core data. So I have an account as a one (the parent), and notes as the many (the children). So really, one account will have many notes. And with this I'm trying to get the notes filtered by way of what their respective account is. To add a bit more complexity to this, some of these views are buried a few layers deep, so they need to have their arguments passed down through each other.
Any insights would be appreciated.
Thank you.
Hi There,
One idea I've just discovered is using a textfield in place of the searchable modifier.
I used a published property wrapper in a class to handle the back and forth of the text entry.
Let me know if you need further clarification.
Cheers.