Posts

Post not yet marked as solved
0 Replies
48 Views
I'm currently trying to save a selected image in core data as a type data, but I'm getting an error in the persistence file marked with a comment. I've included my code and any questions I will gladly answer. Thanks for any help! Content View: import SwiftUI import PhotosUI struct ContentView: View {     @ObservedObject var persistence = PersistenceController.shared     @State var selectedItems: [PhotosPickerItem] = []     @State var data: Data?     var body: some View {         NavigationView{             VStack{                 Spacer()                 VStack{                     Spacer()                     if let data = data, let uiimage = UIImage(data: data) {                         Image(uiImage: uiimage)                             .resizable()                             .scaledToFit()                             .frame(width: 250, height: 500)                     }                     Spacer()                 }                 Spacer()                 PhotosPicker(selection: $selectedItems, maxSelectionCount: 1, matching: .images){                     Text("Pick Photo")                 }                 .onChange(of: selectedItems){ newValue in                     guard let item = selectedItems.first else{                         return                     }                     item.loadTransferable(type: Data.self){ result in                         switch result {                         case .success(let data):                             if let data = data{                                 self.data = data                             } else {                                 print("Data is nil")                             }                         case .failure(let failure):                             fatalError("\(failure)")                         }                     }                 }                 Spacer()             }             .navigationBarItems(trailing: addButton)         }     }     var addButton: some View {         Button(action: {             guard let item = selectedItems.first else{                 return             }             item.loadTransferable(type: Data.self){ result in                 switch result {                 case .success(let data):                     if let data = data{                         persistence.addObject(image: data)                     } else {                         print("Data is nil")                     }                 case .failure(let failure):                     fatalError("\(failure)")                 }             }         }){             Text("Add Image").bold()         }     } } Persistence: import Foundation import CoreData class PersistenceController: ObservableObject {     static let shared = PersistenceController()     let container: NSPersistentContainer     init(inMemory: Bool = false) {         container = NSPersistentContainer(name: "ReciPlanner")         if inMemory {             container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")         }         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         container.viewContext.automaticallyMergesChangesFromParent = true     }          func addObject(image: Data){         let context = container.viewContext         let object = Object(context: context) //Error: Thread 7: "An NSManagedObject of class 'Object' must have a valid NSEntityDescription."         object.item = image     }          func contextSave() {         let context = container.viewContext         if context.hasChanges {             do {                 try context.save()             } catch {                 print("**** ERROR: Unable to save context \(error)")             }         }     } } Data Model:
Posted Last updated
.
Post not yet marked as solved
0 Replies
100 Views
I'm currently trying to save a selected image in core data as a type data, but I'm getting an error in the persistence file marked with a comment. I've included my code and any questions I will gladly answer. Thanks for any help! Content View: import SwiftUI import PhotosUI struct ContentView: View {     @ObservedObject var persistence = PersistenceController.shared     @State var selectedItems: [PhotosPickerItem] = []     @State var data: Data?     var body: some View {         NavigationView{             VStack{                 Spacer()                 VStack{                     Spacer()                     if let data = data, let uiimage = UIImage(data: data) {                         Image(uiImage: uiimage)                             .resizable()                             .scaledToFit()                             .frame(width: 250, height: 500)                     }                     Spacer()                 }                 Spacer()                 PhotosPicker(selection: $selectedItems, maxSelectionCount: 1, matching: .images){                     Text("Pick Photo")                 }                 .onChange(of: selectedItems){ newValue in                     guard let item = selectedItems.first else{                         return                     }                     item.loadTransferable(type: Data.self){ result in                         switch result {                         case .success(let data):                             if let data = data{                                 self.data = data                             } else {                                 print("Data is nil")                             }                         case .failure(let failure):                             fatalError("\(failure)")                         }                     }                 }                 Spacer()             }             .navigationBarItems(trailing: addButton)         }     }     var addButton: some View {         Button(action: {             guard let item = selectedItems.first else{                 return             }             item.loadTransferable(type: Data.self){ result in                 switch result {                 case .success(let data):                     if let data = data{                         persistence.addObject(image: data)                     } else {                         print("Data is nil")                     }                 case .failure(let failure):                     fatalError("\(failure)")                 }             }         }){             Text("Add Image").bold()         }     } } Persistence: import Foundation import CoreData class PersistenceController: ObservableObject {     static let shared = PersistenceController()     let container: NSPersistentContainer     init(inMemory: Bool = false) {         container = NSPersistentContainer(name: "ReciPlanner")         if inMemory {             container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")         }         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         container.viewContext.automaticallyMergesChangesFromParent = true     }          func addObject(image: Data){         let context = container.viewContext         let object = Object(context: context) //Error: Thread 7: "An NSManagedObject of class 'Object' must have a valid NSEntityDescription."         object.item = image     }          func contextSave() {         let context = container.viewContext         if context.hasChanges {             do {                 try context.save()             } catch {                 print("**** ERROR: Unable to save context \(error)")             }         }     } } Data Model:
Posted Last updated
.
Post not yet marked as solved
1 Replies
77 Views
So I have a TabView in my app and I want to go back to the original view when that tabItem is pressed. For example, when you open the App Store and go to the games tab, click on a game. It takes you to a navigation link to a new view. You can either go back to the first page by pressing the arrow, or by tapping the “games” tabItem again. I want to be able to press my tabItem in a subview of the same tab to go back to the original view that the tab is assigned to. Any help would be greatly appreciated! Thanks.
Posted Last updated
.
Post not yet marked as solved
0 Replies
77 Views
So I know this is probably a stretch, but is there any way to airdrop an object from core data? From what I understand airdrop is only used with links, but is there a way to convert the data of a core data object into a file, convert the file into a link, send it via airdrop, and convert it back into a file, and then add that object from a file to another user's core data storage? Any help would be greatly appreciated. Thanks!!
Posted Last updated
.
Post not yet marked as solved
0 Replies
94 Views
Given an image selected in a UIImagePickerController and saved as a UIImage, how can I save that image in a core data model using XCode 14 beta? I’ve only found videos from 2 years ago where they make their own persistence file which is outdated. Any help would be greatly appreciated! Thanks.
Posted Last updated
.
Post not yet marked as solved
0 Replies
48 Views
I'm developing an app that has accounts with usernames and passwords to log in. So far I have found firebase, which doesn't look bad, but I'd like to know every available option and what they have to offer before committing to one. I was curious if Apple had some sort of kit for developers for a login system. Like CloudKit for example, can CloudKit be used to store log in info and be reached when the user is logging in? And if not maybe other options for this. Thanks for any help!
Posted Last updated
.
Post not yet marked as solved
0 Replies
55 Views
I'm developing an app that has accounts with usernames and passwords to log in. So far I have found firebase, which doesn't look bad, but I'd like to know every available option and what they have to offer before committing to one. I was curious if Apple had some sort of kit for developers for a login system. And if not maybe other options for this. Thanks for any help!
Posted Last updated
.
Post not yet marked as solved
0 Replies
94 Views
So I know this is probably a stretch, but is there any way to airdrop an object from core data? From what I understand airdrop is only used with links, but is there a way to convert the data of a core data object into a file, convert the file into a link, send it via airdrop, and convert it back into a file, and then add that object from a file to another user's core data storage? Any help would be greatly appreciated. Thanks!!
Posted Last updated
.
Post not yet marked as solved
0 Replies
62 Views
So I have a TabView in my app and I want to go back yo the main view when that tabItem is pressed. For example, when you open the App Store and go to the games tab, click on a game. It takes you to a navigation link to a new view. You can either go back to the first page by pressing the arrow, or by tapping the “games” tabItem again. I want to be able to press my tabItem in a subview of the same tab to go back to the original view that the tab is assigned to.
Posted Last updated
.
Post not yet marked as solved
0 Replies
98 Views
So I know this is probably a stretch, but is there any way to airdrop an object from core data? From what I understand airdrop is only used with links, but is there a way to convert the data of a core data object into a file, convert the file into a link, send it via airdrop, and convert it back into a file, and then add that object from a file to another user's core data storage? Any help would be greatly appreciated. Thanks!!
Posted Last updated
.
Post not yet marked as solved
1 Replies
87 Views
I need help optimizing and fixing my date list builder. Basically the user should be able to build a list of dates in a sheet and it be shown on the original view and go back to that page to add more. Dates should not be repeated hence why I use a set. Issue: But when the user adds the first date it doesn't show until a second date is added. After some troubleshooting with print I've found that the problem isn't adding the first item to the list, but the ForEach not working until there is a second item. I've included code for a visual representation of my goal. If you have any questions please leave a comment. Any help would be greatly appreciated. Thanks! import SwiftUI struct ContentView: View {     @State private var dates = Set<Date>()     @State private var isDatesShowing = false     var body: some View {         NavigationView{                 Form{                     Section(""){                         addDatesButton                         Section{                             ForEach(dates.sorted(by: <),id: \.self){ date in                                 Text(date, style: .date)                                     .bold()                                     .foregroundColor(.white)                             }                         }                     }                 }                 .navigationBarTitle("List of Dates")                 .sheet(isPresented: $isDatesShowing){                     AddToDateView(dates: $dates)                 }         }         .preferredColorScheme(.dark)     }     var addDatesButton: some View{         Button(action: {             isDatesShowing.toggle()         }){             Text("Select Dates")                 .bold()         }     } } struct AddToDateView: View {     @Environment(\.dismiss) var dismiss     @Binding var dates: Set<Date>     @State var date = Date()          var body: some View {         NavigationView{             Form{                 DatePicker(selection: $date, in: Date.now..., displayedComponents: .date) {                     Text("Select a date")                 }                 .datePickerStyle(WheelDatePickerStyle())                 Button(action:{                     dates.insert(date)                 }){                     Text("Add Date")                 }                 Section("Dates"){                     ForEach(dates.sorted(by: <),id: \.self){ date in                         Button(action: {                             dates.remove(date)                         }){                             Text(date, style: .date)                                 .bold()                                 .foregroundColor(.white)                         }                     }                 }             }             .cornerRadius(10)             .navigationBarItems(trailing: submitButton)                  }     }          var submitButton: some View{         Button(action: {             dismiss()         }){             Text("Submit")                 .bold()         }     } }
Posted Last updated
.
Post not yet marked as solved
1 Replies
106 Views
So this project is pretty straightforward. I have an item. The use can create, get a closer look at, delete, or edit an item. For some reason, no matter how many different ways I try to do it, the edit part does not work. I've tried moving the sheet outside of the context menu (it only opens the bottom item), I've tried moving it under the list using the first index of the item (it crashes), and finally I've tried it the shown way and it just doesn't do anything. If anyone can come up with a better way to open the edit view for the correct item (preferably using a context menu, but something similar is acceptable) I would really appreciate it. I've provided the project for a better understanding of what I'm trying to do. If you have any questions just leave a comment. Any help would be greatly appreciated. Content View: struct ContentView: View {     @Environment(\.managedObjectContext) var managedObjContext     @ObservedObject var persistence = PersistenceController.shared     @State private var items = PersistenceController.shared.getItems()     @State private var showingEditView = false     @State private var showingAddView = false          var body: some View {         NavigationView{             List{                 Section(""){                     ForEach(items) { item in                         NavigationLink(destination: ItemView(item: item)){                             Text(item.name!)                         }                         .contextMenu{                             Button(action: {                                 self.showingEditView.toggle()                             }){                                 Text("Edit Item")                             }                             .sheet(isPresented: $showingEditView){                                 EditItemView(item: item)                                     .onDisappear(perform: {                                         items = persistence.getItems()                                     })                             }                         }                     }                     .onDelete(perform: { indexSet in                         deleteItem(indexSet: indexSet)                     })                 }             }             .listStyle(InsetGroupedListStyle())             .cornerRadius(10)             .navigationBarTitle("My Items")             .navigationBarItems(trailing: addButton)             .onAppear(perform: {                 items = persistence.getItems()             })             .sheet(isPresented: $showingAddView){                 AddItemView()                     .onDisappear(perform: {                         items = persistence.getItems()                     })             }         }     }          var addButton: some View {         Button(action: {             showingAddView.toggle()         }){             Text("Add an Item").bold()         }     }          func deleteItem(indexSet: IndexSet){         withAnimation{             indexSet.map {                 items[$0] }                         .forEach(managedObjContext.delete)                          persistence.contextSave()             items = persistence.getItems()         }     } } Item View: struct ItemView: View{     @State var item: Item     var body: some View{         Text(item.name ?? "No Name")     } } Add View: struct AddItemView: View{     @Environment(\.dismiss) var dismiss     @ObservedObject var persistence = PersistenceController.shared     @State private var name = ""     var body: some View {         Form{             TextField("Item Name", text: $name)             Button(action:{                 persistence.addItem(name: name)                 dismiss()             }){                 Text("Add Item")             }         }     } } Edit View: struct EditItemView: View{     @Environment(\.dismiss) var dismiss     @ObservedObject var persistence = PersistenceController.shared     @State var item: Item     @State private var name = ""     var body: some View {         Form{             TextField("Item Name", text: $name)             Button(action:{                 persistence.addItem(name: name)                 dismiss()             }){                 Text("Add Item")             }         }         .onAppear{             name=item.name ?? "No Name"         }     } } Persistence File: class PersistenceController : ObservableObject{     static let shared = PersistenceController()     let container: NSPersistentContainer          init(inMemory: Bool = false) {         container = NSPersistentContainer(name: "Test")         if inMemory {             container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")         }         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         container.viewContext.automaticallyMergesChangesFromParent = true     }     static var preview: PersistenceController = {         let result = PersistenceController(inMemory: true)         let viewContext = result.container.viewContext         for _ in 0..<10 {             let newItem = Item(context: viewContext)             newItem.id = UUID()             newItem.name = "Test"         }         do {             try viewContext.save()         } catch {             let nsError = error as NSError             fatalError("Unresolved error \(nsError), \(nsError.userInfo)")         }         return result     }()     func getItems() -> [Item] {         let context = container.viewContext         var request = NSFetchRequest<Item>()         request = Item.fetchRequest()         request.entity = NSEntityDescription.entity(forEntityName: "Item", in: context)         do {             let items = try context.fetch(request)             if items.count == 0 { return []}             return items.sorted(by: {$0.name! > $1.name!})         } catch {             print("**** ERROR: items fetch failed \(error)")             return []         }     }          func addItem(name: String){         let context = container.viewContext         let item = Item(context: context)         item.id = UUID()         item.name = name                  contextSave()     }          func contextSave() {         let context = container.viewContext         if context.hasChanges {             do {                 try context.save()                 self.objectWillChange.send()             } catch {                 print("**** ERROR: Unable to save context \(error)")             }         }     } } Data Model:
Posted Last updated
.
Post not yet marked as solved
0 Replies
75 Views
So I have a TabView in my app and I want to go back yo the main view when that tabItem is pressed. For example, when you open the App Store and go to the games tab, click on a game. It takes you to a navigation link to a new view. You can either go back to the first page by pressing the arrow, or by tapping the “games” tabItem again. I want to be able to press my tabItem in a subview of the same tab to go back to the original view that the tab is assigned to.
Posted Last updated
.
Post marked as solved
2 Replies
80 Views
I have an object called Item with two attributes, name (String) and value (Double). Given an array of Items I need combine the values of all items with the same name and keep the items with no duplicates. For example, say there were 4 items in the array and two of them named "Test" and the others "Object" and "Item". "Object" and "Item" would remain in the list, but the values of the two "Test"s would be combined into one item with the same name "Test". I've included the following code for a visual representation. Delete the comments as you read them to clean up. They're just there to clear up any confusion. Leave a comment if you have any questions. Thanks for the help! Content View: import SwiftUI import CoreData struct ContentView: View {     @Environment(\.managedObjectContext) var managedObjContext     @ObservedObject var persistence = PersistenceController.shared     @State private var items = PersistenceController.shared.getItems()          @State var isAddViewShowing = false     var body: some View {         NavigationView{             List{                 Section{                     ForEach(items) { item in //Displays the list of items                         HStack{                             Text(String(item.name!))                             Spacer()                             Text(String(Int(item.value)))                         }                     }                     .onDelete(perform: { indexSet in                         deleteItem(indexSet: indexSet)                     })                 }             }             .navigationBarTitle("Items")             .navigationBarItems(leading: combineItemsButton, trailing: addButton)             .sheet(isPresented: $isAddViewShowing){ //displays the view to add an item                 AddView()                     .onDisappear(perform: {                         items = persistence.getItems() //"refreshes" the list of items                     })             }         }     }          var combineItemsButton: some View{         Button(action:{ //combine duplicates here             persistence.contextSave()             items = persistence.getItems()         }){             Text("Combine Duplicates")                 .bold()         }     }          var addButton: some View{         Button(action:{             isAddViewShowing.toggle()         }){             Text("Add Item")                 .bold()         }     }          func deleteItem(indexSet: IndexSet){         withAnimation{             indexSet.map {                 items[$0]             }             .forEach(managedObjContext.delete)                          persistence.contextSave()             items = persistence.getItems()         }     } } Add View: struct AddView: View{     @Environment(\.dismiss) var dismiss     @ObservedObject var persistence = PersistenceController.shared          @State var name: String = ""     @State var value = ""     @State private var alertMessage = ""     @State private var showAlert = false     var body: some View{         NavigationView{             Form{                 TextField("Item Name", text: $name)                 TextField("Item Value", text: $value)                     .keyboardType(.decimalPad)             }             .navigationBarTitle("Add Item")             .navigationBarItems(leading: dismissButton, trailing: submitButton)         }     }     var submitButton: some View{         Button(action: {             if (name == ""){ //ensures the item has a name                 alertMessage="Your recipe needs a name"                 showAlert.toggle()             } else {                 persistence.addItem(name: name, value: Double(value) ?? 2)                 dismiss()             }         }){             Text("Submit")                 .bold()         }         .alert(alertMessage, isPresented: $showAlert){             Button("OK",role: .cancel){}         }     }          var dismissButton: some View{         Button(action: {             dismiss()         }){             Text("Cancel")                 .bold()         }     } } Persistence File: import CoreData class PersistenceController : ObservableObject{     static let shared = PersistenceController()     let container: NSPersistentContainer          init(inMemory: Bool = false) {         container = NSPersistentContainer(name: "Test")         if inMemory {             container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")         }         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         container.viewContext.automaticallyMergesChangesFromParent = true     }          func getItems() -> [Item] { //fetches items         let context = container.viewContext         var request = NSFetchRequest<Item>()         request = Item.fetchRequest()         request.entity = NSEntityDescription.entity(forEntityName: "Item", in: context)         do {             let items = try context.fetch(request)             if items.count == 0 { return []}             return items.sorted(by: {$0.name! > $1.name!})         } catch {             print("**** ERROR: items fetch failed \(error)")             return []         }     }          func addItem(name: String, value: Double){         let context = container.viewContext         let item = Item(context: context)         item.id = UUID()         item.name = name         item.value = value                  contextSave()     }          func contextSave() {         let context = container.viewContext         if context.hasChanges {             do {                 try context.save()                 self.objectWillChange.send()             } catch {                 print("**** ERROR: Unable to save context \(error)")             }         }     } } Data Model:
Posted Last updated
.
Post not yet marked as solved
2 Replies
252 Views
Basically I need a view with a calendar that will show data attributes from the item. I've tried two different approaches both have their listed problems. There must be a better way to do something like this. Surely it's not ideal to create a new item every time a date is opened or constantly check if something is there, but I don't know any other way. Actual View: import SwiftUI import CoreData struct ContentView: View {     @Environment(\.managedObjectContext) var managedObjContext     @Environment(\.calendar) var calenda     @Environment(\.dismiss) var dismiss     @FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "timestamp == %@", Date.now as CVarArg)) var items: FetchedResults<Item>          @State private var date = Date.now          var body: some View {         NavigationView{             VStack{                 DatePicker("Calendar", selection: $date, in: Date.now...,displayedComponents: [.date])                     .datePickerStyle(.graphical)                     .onAppear(perform: {                         if (items.isEmpty){                             PersistenceController().addItem(date: date, context: managedObjContext)                         }                     })                     .onChange(of: date){ value in                         items.nsPredicate=NSPredicate(format: "timestamp == %@", date as CVarArg)                         if (items.isEmpty){                             PersistenceController().addItem(date: date, context: managedObjContext)                         }                     }                 if (!items.isEmpty){ //This is the only difference in the two approaches. I just put either one of the next two blocks of code in here                 }             }             .navigationBarTitle("My Planner")         }     }          func getTitle(date: Date)->String{         let formatter = DateFormatter()         formatter.dateStyle = .medium         return formatter.string(from: date)     } } First (looks correct, but doesn't show the changes live): PlannedMealsView(item: items[0]) Spacer() //And then this is added at the bottom struct PlannedMealsView: View {     @Environment(\.managedObjectContext) var managedObjContext     @State var item: Item     var body: some View {             VStack{                 Text(item.timestamp ?? Date.now, style: .date)                     .font(.title2)                     .bold()                 Section("Word"){                     if(item.word != nil){                         HStack{                             Spacer()                             Text(item.word!)                             Spacer()                             Button(action: {                                 PersistenceController().removeFromItem(item: item, context: managedObjContext)                             }){                                 Image(systemName: "minus.circle").bold()                             }                             Spacer()                         }                     } else {                         Button(action: {                             PersistenceController().addToItem(item: item, context: managedObjContext)                         }){                             Image(systemName: "plus.circle").bold()                                 .padding(.vertical, 10)                                 .padding(.horizontal, 20)                         }                     }                 }                 Spacer()             }             .frame(height:200)     } } Second (allows direct access to the objects data, but bugs after 5 or 6 date changes): VStack{                             Text(items[0].timestamp ?? Date.now, style: .date)                                 .font(.title2)                                 .bold()                             Section("Word"){                                 if(items[0].word != nil){                                     HStack{                                         Spacer()                                         Text(items[0].word!)                                         Spacer()                                         Button(action: {                                             PersistenceController().removeFromItem(item: items[0], context: managedObjContext)                                         }){                                             Image(systemName: "minus.circle").bold()                                         }                                         Spacer()                                     }                                 } else {                                     Button(action: {                                         PersistenceController().addToItem(item: items[0], context: managedObjContext)                                     }){                                         Image(systemName: "plus.circle").bold()                                             .padding(.vertical, 10)                                             .padding(.horizontal, 20)                                     }                                 }                             }                         Spacer()                     }                     .frame(height:200) Unchanged Files: Persistence- import CoreData struct PersistenceController {     static let shared = PersistenceController()     let container: NSPersistentContainer     init(inMemory: Bool = false) {         container = NSPersistentContainer(name: "Test")         if inMemory {             container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")         }         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         container.viewContext.automaticallyMergesChangesFromParent = true     }          func addItem(date: Date, context: NSManagedObjectContext){         let item = Item(context: context)         item.timestamp = date         item.word = nil                  save(context: context)     }          func addToItem(item: Item, context: NSManagedObjectContext){         item.word = "Test"                  save(context: context)     }          func removeFromItem(item: Item, context: NSManagedObjectContext){         item.word = nil                  save(context: context)     }          func save(context: NSManagedObjectContext){         do {             try context.save()         } catch {             let nsError = error as NSError             fatalError("Unresolved error \(nsError), \(nsError.userInfo)")         }     } } Data Model- If you have any questions I'll be happy to answer. Any help is greatly appreciated. All the best!
Posted Last updated
.