Post

Replies

Boosts

Views

Activity

HealthKit (delete function in SwiftUI)
Hello I am trying to save some data in the Health App from my app, and it is working, the problem is that when I delete that data (already saved) from my app (using the deleteFromHealthKit function) the data is not deleted from the health app. How can I fix this? Here is the code: import SwiftUI import HealthKit struct ContentView: View { init() { //-------- let healthStore = HKHealthStore() let allTypes = Set([HKObjectType.quantityType(forIdentifier: .dietaryWater)!]) healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in if !success { print("success") } } } func fetchHealthData(date: Date, ml: Double) -> Void { let healthStore = HKHealthStore() let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater) let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date) healthStore.save(waterConsumed) { success, error in if (error != nil) { print("Error: \(String(describing: error))") } if success { print("Saved: \(success)") } } } @State var water: [Water] = [] @State private var value: Double = 0 func deleteFromHealthKit(date: Date, ml: Double) { let healthStore = HKHealthStore() let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater) let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date) healthStore.delete(waterConsumed) { success, error in if (error != nil) { print("Error: \(String(describing: error))") } if success { print("Saved: \(success)") } } } var body: some View { NavigationView{ VStack{ Text("Value: \(value)") .padding() HStack{ Text("100 ml") .onTapGesture { value = 100 } Text("200 ml") .onTapGesture { value = 200 } } Button("Add"){ water.append(Water(value: value, date: Date())) fetchHealthData(date: Date(), ml: value) }.disabled(value == 0 ? true : false) .padding() List{ ForEach(0..<water.count, id: \.self){ i in HStack{ Text("\(water[i].value)") Text("\(water[i].date)") } .onTapGesture { deleteFromHealthKit(date: water[i].date, ml: water[i].value) water.remove(at: i) } } } } } } } struct Water: Identifiable { var id = UUID() var value: Double var date: Date } Thank you
2
0
1.6k
Jun ’21
Sync iCloud with different targets
Hello Is there a way to share data stored in CloudKit and CoreData between iOS and watchOS in SwiftUI? I am using the same CoreData file both, and I am using the same PersistenceController file both, and I am using the same CloudKit container for both. I tried adding the App Groups capability to all the targets, but it is not working. (I already enabled iCloud capability, Push notification capability and background Modes capability for both iOS and WatchOS)  PersistenceController: struct PersistenceController { static let shared = PersistenceController() static var preview: PersistenceController = { let result = PersistenceController(inMemory: true) let viewContext = result.container.viewContext do { try viewContext.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } return result }() let container: NSPersistentCloudKitContainer init(inMemory: Bool = false) { container = NSPersistentCloudKitContainer(name: "Test7") 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 container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy } } View: struct ContentView: View { @FetchRequest( entity: Item.entity(), sortDescriptors: [] ) var items: FetchedResults<Item> @Environment(\.managedObjectContext) private var context var body: some View { NavigationView{ List { ForEach(items) { item in Text("Item at \(item.date!)") } .onDelete(perform: deleteItems) } .toolbar { Button { addItem() } label: { Image(systemName: "plus") } } } } private func addItem() { withAnimation { let newItem = Item(context: context) newItem.date = Date() do { try context.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } } } private func deleteItems(offsets: IndexSet) { withAnimation { offsets.map { items[$0] }.forEach(context.delete) do { try context.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } } } } Thank you
0
0
658
Jun ’21
User Notifications in SwiftUI
Hello How can I set up local notifications that repeat every X minutes from a certain hour to another hour. For example, I want to receive a notification every 60 minutes starting from 12:00 AM to 10:00 PM? func scheduleNotifications() { let content = UNMutableNotificationContent() content.title = "App" content.subtitle = "App" content.sound = UNNotificationSound.default let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true) let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) } With this code I can schedule a notification every minute, however, I cannot decide from what time it should start or when it stops. Any ideas? Thank you!
0
0
1.3k
Jun ’21
onDelete not working properly in my app
Hello In my app I have a list of items in a ForEach and at the end of the ForEach there is a .onDelete. (This is in a View that I called AllHistoryView) The items are represented by a CoreData NSManagedObject that I called LifetimeInputs with 3 properties: date: Date, imageTemplate: String, valori: Double The problem is that, when I add (in a different View) an item with the date of tomorrow and then I add another to item with the date of today and then go to the AllHistoryView where there are all the items and delete slowly the item with date of tomorrow (especially at the end of the swipe action) instead of deleting the item that I swiped it deletes the on before it, how can I solve this problem? Here is the AllHistoryView code: import SwiftUI import CoreData struct AllHistoryView: View { @Environment(\.managedObjectContext) var viewContext @FetchRequest(entity: LifetimeInputs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], animation: .default) var lifetimeInputsModel: FetchedResults<LifetimeInputs> @State private var text: String = "" @State private var ascending: Bool = true @State var sortDescriptor: NSSortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true) @Environment(\.dynamicTypeSize) var dynamicTypeSize var size: CGFloat{ if UIDevice.current.userInterfaceIdiom == .phone { switch dynamicTypeSize { case .xSmall: return 11 case .small: return 13 case .medium: return 15 case .large: return 17 case .xLarge: return 19 case .xxLarge: return 21 case .xxxLarge: return 23 default: return 23 } } else { switch dynamicTypeSize { case .xSmall: return 13 case .small: return 15 case .medium: return 17 case .large: return 19 case .xLarge: return 21 case .xxLarge: return 23 case .xxxLarge: return 25 case .accessibility1: return 27 case .accessibility2: return 29 default: return 29 } } } var body: some View { theView() } @ViewBuilder func theView() -> some View{ NavigationView{ if !lifetimeInputsModel.isEmpty{ List{ SectionList(sortDescripter: sortDescriptor, text: $text) .environment(\.managedObjectContext, viewContext) .onChange(of: ascending, perform: { _ in if ascending { withAnimation { sortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true) } } else { withAnimation { sortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: false) } } }) } .searchable(text: $text, placement: .navigationBarDrawer, prompt: "Quantity or date".localized()) .navigationBarTitle("History", displayMode: .inline) .toolbar{ ToolbarItem(placement: .automatic) { Image(systemName: "arrow.up.arrow.down.circle") .foregroundColor(.primary) .font(.system(size: size)) .rotation3DEffect(.degrees(ascending ? 0 : 180), axis: (x: 1, y: 0, z: 0)) .opacity(0.5) .onTapGesture { ascending.toggle() } } } } else{ VStack{ Text("No Data".localized()) .font(.largeTitle) .fontWeight(.semibold) .foregroundColor(.secondary) } .padding(.bottom) .navigationBarTitle("History", displayMode: .inline) } } } } struct SectionList: View { @Environment(\.managedObjectContext) var viewContext @FetchRequest var lifetimeInputsModel: FetchedResults<LifetimeInputs> @Binding var text: String init(sortDescripter: NSSortDescriptor, text: Binding<String>) { let request: NSFetchRequest<LifetimeInputs> = LifetimeInputs.fetchRequest() request.sortDescriptors = [sortDescripter] _lifetimeInputsModel = FetchRequest<LifetimeInputs>(fetchRequest: request) self._text = text } @FetchRequest(entity: Limit.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Limit.date, ascending: true)], animation: .default) var limit: FetchedResults<Limit> @Environment(\.dynamicTypeSize) var dynamicTypeSize var size: CGFloat{ if UIDevice.current.userInterfaceIdiom == .phone { switch dynamicTypeSize { case .xSmall: return 11 case .small: return 13 case .medium: return 15 case .large: return 17 case .xLarge: return 19 case .xxLarge: return 21 case .xxxLarge: return 23 default: return 23 } } else { switch dynamicTypeSize { case .xSmall: return 13 case .small: return 15 case .medium: return 17 case .large: return 19 case .xLarge: return 21 case .xxLarge: return 23 case .xxxLarge: return 25 case .accessibility1: return 27 case .accessibility2: return 29 default: return 29 } } } @StateObject var lifeTimeInputsViewModel = LifeTimeInputsViewModel() var body: some View { Section{ ForEach(lifetimeInputsModel.filter { text.isEmpty || "\($0)".contains(text) }){ lifetimeInputs in HStack{ Text("\(lifetimeInputs.valori, specifier: format(unita: !limit.isEmpty ? limit[0].unita ?? ml : ml)) \(!limit.isEmpty ? limit[0].unita ?? ml: ml)") .font(.system(size: size)) Spacer() Text("\(dateFormatter.string(from: lifetimeInputs.date ?? Date()))") .font(.system(size: size)) } } .onDelete(perform: { offsets in lifeTimeInputsViewModel.deleteItems(offsets: offsets) }) } header: { Text("History".localized()).font(.system(size: size - 4)) } } } Thank You very much!
0
0
761
Sep ’21
Notify when @FetchRequest changes?
Hello @FetchRequest( entity: Book(), sortDescriptors: [] ) var books: FetchedResults<Book> How can I get notified when books changes? I was thinking about putting this... .onReceive(NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave), perform: { _ in}) ... in my main View but this would notify me if anything gets saved and that is not what I want. I want to get notified just if a certain FetchRequest changes (in this case books)? And in another thread I was told to use... .onRecevie(books.publisher){ _ in .... } ...however using this code publishes each member of the sequence as a separate element, I want it to publish just once? Thank You!
0
0
586
Sep ’21
@propertywrapper in SwiftUI
Hello I created a @propertyWrapper to limit the number a variable can reach. I tried it in a SwiftUI view with a button that increases the value of the variable and it works, the variable stops at the maximum number set in the initializer. However if I try it with a Textflied it doesn't work, if I insert a higher number than the one set nothing happens, it makes me do it. How can I solve this problem, I know the problem has to do with Binding but I don't know exactly what it is, here is the code: import SwiftUI struct ContentView: View { @Maximum(maximum: 12) var quantity: Int var body: some View { NavigationView{ Form{ TextField("", value: $quantity, format: .number, prompt: Text("Pizza").foregroundColor(.red)) Button { quantity += 1 } label: { Text("\(quantity)") } } } } } @propertyWrapper struct Maximum<T: Comparable> where T: Numeric { @State private var number: T = 0 var max: T var wrappedValue: T { get { number } nonmutating set { number = min(newValue, max) } } var projectedValue: Binding<T> { Binding( get: { wrappedValue }, set: { wrappedValue = $0 } ) } init(maximum: T){ max = maximum } } extension Maximum: DynamicProperty { } Thank You for your time
0
0
972
Dec ’21
Scheduling local notifications to repeat daily from tomorrow
Hello I'm trying to schedule a local notification to fire every day, at a specific time, but from tomorrow. e.g. "Trigger a notification every day at 2 pm, from tomorrow" This is how I set up my schedule function. func scheduleNotifications(date: Date, identfier: String, after: Bool) { let content = UNMutableNotificationContent() content.title = "App" content.body = "Test" content.sound = .default content.userInfo = ["Hour": Int(hourFormatter.string(from: date)) ?? 0] let afterDay = Calendar.current.date(byAdding: .day, value: after ? 1 : 0, to: Date()) var components = Calendar.current.dateComponents([.hour, .minute], from: afterDay!) components.hour = Int(hourFormatter.string(from: date)) ?? 0 components.minute = Int(minuteFormatter.string(from: date)) ?? 0 let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true) let request = UNNotificationRequest(identifier: identfier, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) }
1
0
1.9k
Dec ’21
Fetch data from CloudKit to app when app is in background
Hello I have an app that uses Core Data with CloudKit to store data. When I am in the app (using the app) and create some new data on an other device, the data is fetched in a few seconds and I see it immediately, however if I am not using the app and create some new data on an other device, I have to enter the app and then the data starts fetching, is there a way to fetch data even if I am not using the app. Here is my core data stack: import CoreData import Combine class PersistenceController { static let shared = PersistenceController() let container: NSPersistentCloudKitContainer init() { container = NSPersistentCloudKitContainer(name: "CoreData") guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_NAME")?.appendingPathComponent("CoreData.sqlite") else { fatalError("Shared file container could not be created.") } let storeDescription = NSPersistentStoreDescription(url: fileContainer) storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) storeDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "Container_Identifier") container.persistentStoreDescriptions = [storeDescription] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy } }
1
0
1.8k
Jan ’22
Filter store transactions Core Data
Hello I have this Core Data stack and I have an observer to observe NSPersistentStoreRemoteChange, I would like to filter changes that the user has made to call the mergePersistentHistoryChanges() just when needed, I think it has to be done in the fetchPersistentHistoryTransactionsAndChanges() function but I don't know how to do it. Can you help me. Thank You Here is my Core Data Stack: class PersistenceController { static let shared = PersistenceController() private var notificationToken: NSObjectProtocol? init() { notificationToken = NotificationCenter.default.addObserver(forName: .NSPersistentStoreRemoteChange, object: nil, queue: nil) { note in Task { await self.fetchPersistentHistory() } } } deinit { if let observer = notificationToken { NotificationCenter.default.removeObserver(observer) } } private var lastToken: NSPersistentHistoryToken? /// A persistent container to set up the Core Data stack. lazy var container: NSPersistentCloudKitContainer = { let fileContainer = URL.storeURL(for: "group name", databaseName: "CoreData") let container = NSPersistentCloudKitContainer(name: "CoreData") let defaultDirectoryURL = NSPersistentContainer.defaultDirectoryURL() let localStoreURL = defaultDirectoryURL.appendingPathComponent("Local.sqlite") let localStoreDescription = NSPersistentStoreDescription(url: localStoreURL) localStoreDescription.configuration = "Local" // Create a store description for a CloudKit-backed local store let cloudStoreDescription = NSPersistentStoreDescription(url: fileContainer) cloudStoreDescription.configuration = "Cloud" // Set the container options on the cloud store cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "containerIdentifier") cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) container.persistentStoreDescriptions = [cloudStoreDescription, localStoreDescription] container.loadPersistentStores { storeDescription, error in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } } // This sample refreshes UI by consuming store changes via persistent history tracking. /// - Tag: viewContextMergeParentChanges container.viewContext.automaticallyMergesChangesFromParent = false container.viewContext.name = "viewContext" container.viewContext.transactionAuthor = "User" /// - Tag: viewContextMergePolicy container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy container.viewContext.undoManager = nil container.viewContext.shouldDeleteInaccessibleFaults = true return container }() private func newTaskContext() -> NSManagedObjectContext { // Create a private queue context. /// - Tag: newBackgroundContext let taskContext = container.newBackgroundContext() taskContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy // Set unused undoManager to nil for macOS (it is nil by default on iOS) // to reduce resource requirements. taskContext.undoManager = nil return taskContext } func save() { if self.container.viewContext.hasChanges { do { try self.container.viewContext.save() } catch { print(Errors.errorSaving) } } } func fetchPersistentHistory() async { do { try await fetchPersistentHistoryTransactionsAndChanges() } catch { print(Errors.fetchPersistentHistory) } } private func fetchPersistentHistoryTransactionsAndChanges() async throws { let taskContext = newTaskContext() taskContext.name = "persistentHistoryContext" try await taskContext.perform { let changeRequest = NSPersistentHistoryChangeRequest.fetchHistory(after: self.lastToken) let historyResult = try taskContext.execute(changeRequest) as? NSPersistentHistoryResult if let history = historyResult?.result as? [NSPersistentHistoryTransaction], !history.isEmpty { self.mergePersistentHistoryChanges(from: history) return } throw Errors.fetchPersistentHistoryTransactionsAndChanges } } private func mergePersistentHistoryChanges(from history: [NSPersistentHistoryTransaction]) { let viewContext = container.viewContext viewContext.perform { for transaction in history { print("Merged by func mergePersistentHistoryChanges ") viewContext.mergeChanges(fromContextDidSave: transaction.objectIDNotification()) self.lastToken = transaction.token } } } }
0
0
987
Jan ’22
Convert func to use async
Hello I have a function to add data to Core Data that has a completion and uses Futures, I wanted to know what is the best way to convert it to use async, because I just started learning how to use async and await. (I am not really sure how to do it) Here is the function: func add(context: NSManagedObjectContext, _ body: @escaping (inout Entity) -> Void) -> AnyPublisher<Entity, Error> { Deferred { [context] in Future { promise in context.perform { var entity = Entity(context: context) body(&entity) do { try context.save() promise(.success(entity)) } catch { promise(.failure(error)) } } } } .eraseToAnyPublisher() } Thank You
0
0
890
Jan ’22
Indoor Geofencing (SensorKit)
Hello What is the best framework for indoor geofencing (with iBeacons devices)? Would the SensorKit framework be any good? I'm trying to build an indoor geofencing app that uses UWB or BLE devices. I am thinking of the SensorKit framework because it is used for research and studies and I am looking for the best way to realize indoor geofencing (preferably using UWB devices). The app should also work while it's in the background. Thank you!
0
0
952
Jul ’22
Access all data in a CoreData store as a developer
Hello I am building a study research app that tracks data of different users. The data is saved through Core Data (with relationships) and I was wondering if there was a way to view all the data through CloudKit? I know I can use public databases with CloudKit in CoreData but was wondering if that would merge all the data of the different users, and if that is the case how would I deal with it? If that is not possible how would I download all the data in a PDF that the user can send? Thanks for your help
1
0
861
Jan ’23
The document “iosfwd” could not be saved. You don’t have permission.
Hello Everytime I run a project I get this popup: The document “iosfwd” could not be saved. You don’t have permission. How do I fix it? Thank you
Replies
4
Boosts
0
Views
1.4k
Activity
Aug ’21
Run function at certain moment, SwiftUI
Hello Is there a way to execute a function (even if the app is closed) at a certain time, for example 12:30 AM?
Replies
0
Boosts
0
Views
466
Activity
May ’21
HealthKit (delete function in SwiftUI)
Hello I am trying to save some data in the Health App from my app, and it is working, the problem is that when I delete that data (already saved) from my app (using the deleteFromHealthKit function) the data is not deleted from the health app. How can I fix this? Here is the code: import SwiftUI import HealthKit struct ContentView: View { init() { //-------- let healthStore = HKHealthStore() let allTypes = Set([HKObjectType.quantityType(forIdentifier: .dietaryWater)!]) healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in if !success { print("success") } } } func fetchHealthData(date: Date, ml: Double) -> Void { let healthStore = HKHealthStore() let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater) let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date) healthStore.save(waterConsumed) { success, error in if (error != nil) { print("Error: \(String(describing: error))") } if success { print("Saved: \(success)") } } } @State var water: [Water] = [] @State private var value: Double = 0 func deleteFromHealthKit(date: Date, ml: Double) { let healthStore = HKHealthStore() let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater) let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date) healthStore.delete(waterConsumed) { success, error in if (error != nil) { print("Error: \(String(describing: error))") } if success { print("Saved: \(success)") } } } var body: some View { NavigationView{ VStack{ Text("Value: \(value)") .padding() HStack{ Text("100 ml") .onTapGesture { value = 100 } Text("200 ml") .onTapGesture { value = 200 } } Button("Add"){ water.append(Water(value: value, date: Date())) fetchHealthData(date: Date(), ml: value) }.disabled(value == 0 ? true : false) .padding() List{ ForEach(0..<water.count, id: \.self){ i in HStack{ Text("\(water[i].value)") Text("\(water[i].date)") } .onTapGesture { deleteFromHealthKit(date: water[i].date, ml: water[i].value) water.remove(at: i) } } } } } } } struct Water: Identifiable { var id = UUID() var value: Double var date: Date } Thank you
Replies
2
Boosts
0
Views
1.6k
Activity
Jun ’21
Sync iCloud with different targets
Hello Is there a way to share data stored in CloudKit and CoreData between iOS and watchOS in SwiftUI? I am using the same CoreData file both, and I am using the same PersistenceController file both, and I am using the same CloudKit container for both. I tried adding the App Groups capability to all the targets, but it is not working. (I already enabled iCloud capability, Push notification capability and background Modes capability for both iOS and WatchOS)  PersistenceController: struct PersistenceController { static let shared = PersistenceController() static var preview: PersistenceController = { let result = PersistenceController(inMemory: true) let viewContext = result.container.viewContext do { try viewContext.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } return result }() let container: NSPersistentCloudKitContainer init(inMemory: Bool = false) { container = NSPersistentCloudKitContainer(name: "Test7") 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 container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy } } View: struct ContentView: View { @FetchRequest( entity: Item.entity(), sortDescriptors: [] ) var items: FetchedResults<Item> @Environment(\.managedObjectContext) private var context var body: some View { NavigationView{ List { ForEach(items) { item in Text("Item at \(item.date!)") } .onDelete(perform: deleteItems) } .toolbar { Button { addItem() } label: { Image(systemName: "plus") } } } } private func addItem() { withAnimation { let newItem = Item(context: context) newItem.date = Date() do { try context.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } } } private func deleteItems(offsets: IndexSet) { withAnimation { offsets.map { items[$0] }.forEach(context.delete) do { try context.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } } } } Thank you
Replies
0
Boosts
0
Views
658
Activity
Jun ’21
User Notifications in SwiftUI
Hello How can I set up local notifications that repeat every X minutes from a certain hour to another hour. For example, I want to receive a notification every 60 minutes starting from 12:00 AM to 10:00 PM? func scheduleNotifications() { let content = UNMutableNotificationContent() content.title = "App" content.subtitle = "App" content.sound = UNNotificationSound.default let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true) let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) } With this code I can schedule a notification every minute, however, I cannot decide from what time it should start or when it stops. Any ideas? Thank you!
Replies
0
Boosts
0
Views
1.3k
Activity
Jun ’21
Date in Swift printed is wrong
Hello In my previous post I asked: https://developer.apple.com/forums/thread/689720 (Read my previous post before continuing, please) My question now is, if I compare Dates between each other will they be formatted or not, and how would I fix this? Thank You
Replies
2
Boosts
0
Views
638
Activity
Sep ’21
onDelete not working properly in my app
Hello In my app I have a list of items in a ForEach and at the end of the ForEach there is a .onDelete. (This is in a View that I called AllHistoryView) The items are represented by a CoreData NSManagedObject that I called LifetimeInputs with 3 properties: date: Date, imageTemplate: String, valori: Double The problem is that, when I add (in a different View) an item with the date of tomorrow and then I add another to item with the date of today and then go to the AllHistoryView where there are all the items and delete slowly the item with date of tomorrow (especially at the end of the swipe action) instead of deleting the item that I swiped it deletes the on before it, how can I solve this problem? Here is the AllHistoryView code: import SwiftUI import CoreData struct AllHistoryView: View { @Environment(\.managedObjectContext) var viewContext @FetchRequest(entity: LifetimeInputs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], animation: .default) var lifetimeInputsModel: FetchedResults<LifetimeInputs> @State private var text: String = "" @State private var ascending: Bool = true @State var sortDescriptor: NSSortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true) @Environment(\.dynamicTypeSize) var dynamicTypeSize var size: CGFloat{ if UIDevice.current.userInterfaceIdiom == .phone { switch dynamicTypeSize { case .xSmall: return 11 case .small: return 13 case .medium: return 15 case .large: return 17 case .xLarge: return 19 case .xxLarge: return 21 case .xxxLarge: return 23 default: return 23 } } else { switch dynamicTypeSize { case .xSmall: return 13 case .small: return 15 case .medium: return 17 case .large: return 19 case .xLarge: return 21 case .xxLarge: return 23 case .xxxLarge: return 25 case .accessibility1: return 27 case .accessibility2: return 29 default: return 29 } } } var body: some View { theView() } @ViewBuilder func theView() -> some View{ NavigationView{ if !lifetimeInputsModel.isEmpty{ List{ SectionList(sortDescripter: sortDescriptor, text: $text) .environment(\.managedObjectContext, viewContext) .onChange(of: ascending, perform: { _ in if ascending { withAnimation { sortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true) } } else { withAnimation { sortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: false) } } }) } .searchable(text: $text, placement: .navigationBarDrawer, prompt: "Quantity or date".localized()) .navigationBarTitle("History", displayMode: .inline) .toolbar{ ToolbarItem(placement: .automatic) { Image(systemName: "arrow.up.arrow.down.circle") .foregroundColor(.primary) .font(.system(size: size)) .rotation3DEffect(.degrees(ascending ? 0 : 180), axis: (x: 1, y: 0, z: 0)) .opacity(0.5) .onTapGesture { ascending.toggle() } } } } else{ VStack{ Text("No Data".localized()) .font(.largeTitle) .fontWeight(.semibold) .foregroundColor(.secondary) } .padding(.bottom) .navigationBarTitle("History", displayMode: .inline) } } } } struct SectionList: View { @Environment(\.managedObjectContext) var viewContext @FetchRequest var lifetimeInputsModel: FetchedResults<LifetimeInputs> @Binding var text: String init(sortDescripter: NSSortDescriptor, text: Binding<String>) { let request: NSFetchRequest<LifetimeInputs> = LifetimeInputs.fetchRequest() request.sortDescriptors = [sortDescripter] _lifetimeInputsModel = FetchRequest<LifetimeInputs>(fetchRequest: request) self._text = text } @FetchRequest(entity: Limit.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Limit.date, ascending: true)], animation: .default) var limit: FetchedResults<Limit> @Environment(\.dynamicTypeSize) var dynamicTypeSize var size: CGFloat{ if UIDevice.current.userInterfaceIdiom == .phone { switch dynamicTypeSize { case .xSmall: return 11 case .small: return 13 case .medium: return 15 case .large: return 17 case .xLarge: return 19 case .xxLarge: return 21 case .xxxLarge: return 23 default: return 23 } } else { switch dynamicTypeSize { case .xSmall: return 13 case .small: return 15 case .medium: return 17 case .large: return 19 case .xLarge: return 21 case .xxLarge: return 23 case .xxxLarge: return 25 case .accessibility1: return 27 case .accessibility2: return 29 default: return 29 } } } @StateObject var lifeTimeInputsViewModel = LifeTimeInputsViewModel() var body: some View { Section{ ForEach(lifetimeInputsModel.filter { text.isEmpty || "\($0)".contains(text) }){ lifetimeInputs in HStack{ Text("\(lifetimeInputs.valori, specifier: format(unita: !limit.isEmpty ? limit[0].unita ?? ml : ml)) \(!limit.isEmpty ? limit[0].unita ?? ml: ml)") .font(.system(size: size)) Spacer() Text("\(dateFormatter.string(from: lifetimeInputs.date ?? Date()))") .font(.system(size: size)) } } .onDelete(perform: { offsets in lifeTimeInputsViewModel.deleteItems(offsets: offsets) }) } header: { Text("History".localized()).font(.system(size: size - 4)) } } } Thank You very much!
Replies
0
Boosts
0
Views
761
Activity
Sep ’21
Notify when @FetchRequest changes?
Hello @FetchRequest( entity: Book(), sortDescriptors: [] ) var books: FetchedResults<Book> How can I get notified when books changes? I was thinking about putting this... .onReceive(NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave), perform: { _ in}) ... in my main View but this would notify me if anything gets saved and that is not what I want. I want to get notified just if a certain FetchRequest changes (in this case books)? And in another thread I was told to use... .onRecevie(books.publisher){ _ in .... } ...however using this code publishes each member of the sequence as a separate element, I want it to publish just once? Thank You!
Replies
0
Boosts
0
Views
586
Activity
Sep ’21
@propertywrapper in SwiftUI
Hello I created a @propertyWrapper to limit the number a variable can reach. I tried it in a SwiftUI view with a button that increases the value of the variable and it works, the variable stops at the maximum number set in the initializer. However if I try it with a Textflied it doesn't work, if I insert a higher number than the one set nothing happens, it makes me do it. How can I solve this problem, I know the problem has to do with Binding but I don't know exactly what it is, here is the code: import SwiftUI struct ContentView: View { @Maximum(maximum: 12) var quantity: Int var body: some View { NavigationView{ Form{ TextField("", value: $quantity, format: .number, prompt: Text("Pizza").foregroundColor(.red)) Button { quantity += 1 } label: { Text("\(quantity)") } } } } } @propertyWrapper struct Maximum<T: Comparable> where T: Numeric { @State private var number: T = 0 var max: T var wrappedValue: T { get { number } nonmutating set { number = min(newValue, max) } } var projectedValue: Binding<T> { Binding( get: { wrappedValue }, set: { wrappedValue = $0 } ) } init(maximum: T){ max = maximum } } extension Maximum: DynamicProperty { } Thank You for your time
Replies
0
Boosts
0
Views
972
Activity
Dec ’21
Scheduling local notifications to repeat daily from tomorrow
Hello I'm trying to schedule a local notification to fire every day, at a specific time, but from tomorrow. e.g. "Trigger a notification every day at 2 pm, from tomorrow" This is how I set up my schedule function. func scheduleNotifications(date: Date, identfier: String, after: Bool) { let content = UNMutableNotificationContent() content.title = "App" content.body = "Test" content.sound = .default content.userInfo = ["Hour": Int(hourFormatter.string(from: date)) ?? 0] let afterDay = Calendar.current.date(byAdding: .day, value: after ? 1 : 0, to: Date()) var components = Calendar.current.dateComponents([.hour, .minute], from: afterDay!) components.hour = Int(hourFormatter.string(from: date)) ?? 0 components.minute = Int(minuteFormatter.string(from: date)) ?? 0 let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true) let request = UNNotificationRequest(identifier: identfier, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) }
Replies
1
Boosts
0
Views
1.9k
Activity
Dec ’21
Fetch data from CloudKit to app when app is in background
Hello I have an app that uses Core Data with CloudKit to store data. When I am in the app (using the app) and create some new data on an other device, the data is fetched in a few seconds and I see it immediately, however if I am not using the app and create some new data on an other device, I have to enter the app and then the data starts fetching, is there a way to fetch data even if I am not using the app. Here is my core data stack: import CoreData import Combine class PersistenceController { static let shared = PersistenceController() let container: NSPersistentCloudKitContainer init() { container = NSPersistentCloudKitContainer(name: "CoreData") guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_NAME")?.appendingPathComponent("CoreData.sqlite") else { fatalError("Shared file container could not be created.") } let storeDescription = NSPersistentStoreDescription(url: fileContainer) storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) storeDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "Container_Identifier") container.persistentStoreDescriptions = [storeDescription] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy } }
Replies
1
Boosts
0
Views
1.8k
Activity
Jan ’22
Filter store transactions Core Data
Hello I have this Core Data stack and I have an observer to observe NSPersistentStoreRemoteChange, I would like to filter changes that the user has made to call the mergePersistentHistoryChanges() just when needed, I think it has to be done in the fetchPersistentHistoryTransactionsAndChanges() function but I don't know how to do it. Can you help me. Thank You Here is my Core Data Stack: class PersistenceController { static let shared = PersistenceController() private var notificationToken: NSObjectProtocol? init() { notificationToken = NotificationCenter.default.addObserver(forName: .NSPersistentStoreRemoteChange, object: nil, queue: nil) { note in Task { await self.fetchPersistentHistory() } } } deinit { if let observer = notificationToken { NotificationCenter.default.removeObserver(observer) } } private var lastToken: NSPersistentHistoryToken? /// A persistent container to set up the Core Data stack. lazy var container: NSPersistentCloudKitContainer = { let fileContainer = URL.storeURL(for: "group name", databaseName: "CoreData") let container = NSPersistentCloudKitContainer(name: "CoreData") let defaultDirectoryURL = NSPersistentContainer.defaultDirectoryURL() let localStoreURL = defaultDirectoryURL.appendingPathComponent("Local.sqlite") let localStoreDescription = NSPersistentStoreDescription(url: localStoreURL) localStoreDescription.configuration = "Local" // Create a store description for a CloudKit-backed local store let cloudStoreDescription = NSPersistentStoreDescription(url: fileContainer) cloudStoreDescription.configuration = "Cloud" // Set the container options on the cloud store cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "containerIdentifier") cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) container.persistentStoreDescriptions = [cloudStoreDescription, localStoreDescription] container.loadPersistentStores { storeDescription, error in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } } // This sample refreshes UI by consuming store changes via persistent history tracking. /// - Tag: viewContextMergeParentChanges container.viewContext.automaticallyMergesChangesFromParent = false container.viewContext.name = "viewContext" container.viewContext.transactionAuthor = "User" /// - Tag: viewContextMergePolicy container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy container.viewContext.undoManager = nil container.viewContext.shouldDeleteInaccessibleFaults = true return container }() private func newTaskContext() -> NSManagedObjectContext { // Create a private queue context. /// - Tag: newBackgroundContext let taskContext = container.newBackgroundContext() taskContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy // Set unused undoManager to nil for macOS (it is nil by default on iOS) // to reduce resource requirements. taskContext.undoManager = nil return taskContext } func save() { if self.container.viewContext.hasChanges { do { try self.container.viewContext.save() } catch { print(Errors.errorSaving) } } } func fetchPersistentHistory() async { do { try await fetchPersistentHistoryTransactionsAndChanges() } catch { print(Errors.fetchPersistentHistory) } } private func fetchPersistentHistoryTransactionsAndChanges() async throws { let taskContext = newTaskContext() taskContext.name = "persistentHistoryContext" try await taskContext.perform { let changeRequest = NSPersistentHistoryChangeRequest.fetchHistory(after: self.lastToken) let historyResult = try taskContext.execute(changeRequest) as? NSPersistentHistoryResult if let history = historyResult?.result as? [NSPersistentHistoryTransaction], !history.isEmpty { self.mergePersistentHistoryChanges(from: history) return } throw Errors.fetchPersistentHistoryTransactionsAndChanges } } private func mergePersistentHistoryChanges(from history: [NSPersistentHistoryTransaction]) { let viewContext = container.viewContext viewContext.perform { for transaction in history { print("Merged by func mergePersistentHistoryChanges ") viewContext.mergeChanges(fromContextDidSave: transaction.objectIDNotification()) self.lastToken = transaction.token } } } }
Replies
0
Boosts
0
Views
987
Activity
Jan ’22
Convert func to use async
Hello I have a function to add data to Core Data that has a completion and uses Futures, I wanted to know what is the best way to convert it to use async, because I just started learning how to use async and await. (I am not really sure how to do it) Here is the function: func add(context: NSManagedObjectContext, _ body: @escaping (inout Entity) -> Void) -> AnyPublisher<Entity, Error> { Deferred { [context] in Future { promise in context.perform { var entity = Entity(context: context) body(&entity) do { try context.save() promise(.success(entity)) } catch { promise(.failure(error)) } } } } .eraseToAnyPublisher() } Thank You
Replies
0
Boosts
0
Views
890
Activity
Jan ’22
Indoor Geofencing (SensorKit)
Hello What is the best framework for indoor geofencing (with iBeacons devices)? Would the SensorKit framework be any good? I'm trying to build an indoor geofencing app that uses UWB or BLE devices. I am thinking of the SensorKit framework because it is used for research and studies and I am looking for the best way to realize indoor geofencing (preferably using UWB devices). The app should also work while it's in the background. Thank you!
Replies
0
Boosts
0
Views
952
Activity
Jul ’22
Access all data in a CoreData store as a developer
Hello I am building a study research app that tracks data of different users. The data is saved through Core Data (with relationships) and I was wondering if there was a way to view all the data through CloudKit? I know I can use public databases with CloudKit in CoreData but was wondering if that would merge all the data of the different users, and if that is the case how would I deal with it? If that is not possible how would I download all the data in a PDF that the user can send? Thanks for your help
Replies
1
Boosts
0
Views
861
Activity
Jan ’23