Post

Replies

Boosts

Views

Activity

What would be the best logic for updating a file periodically after an app has been released
I have a code that needs to be ran about every year to update some files at the app launch and I'm trying to come up with the best way to do that. What I need to be able to basically force the app to run the code whenever I need to update the files. This is what I came up with that I think would work but I'm not sure if that's the best way. Are there any other options to handle this type of logic? App version 1 - INITIAL RELASE. This code would work as long as I don't add an update. // First launch @main struct SwifUIPlayGroundApp: App { @AppStorage("shouldLoad") var shouldLoad = true init(){ if shouldLoad{ print("Loading...") shouldLoad = false }else{ print("No need to relaod...") } } } App version 2 - UPDATE 1. Here I would need to add a second variable to force the update shouldUpdate. I would also need to change the logic to check for the shouldUpdate instead of the shouldLoad and set the shouldLoad to true to be prepared for future updates. // UPDATE 1 @main struct SwifUIPlayGroundApp: App { @AppStorage("shouldUpdate") var shouldUpdate = true // new @AppStorage("shouldLoad") var shouldLoad = true init(){ if shouldUpdate{ print("Loading...") shouldUpdate = false shouldLoad = true // prepare for next update }else{ print("No need to relaod...") } } } App version 3 - UPDATE 2. Here I would need to change the logic back to check for the shouldLoad instead of the shouldUpdate and set the shouldUpdate to true to be prepared for future updates. // UPDATE 2 @main struct SwifUIPlayGroundApp: App { @AppStorage("shouldUpdate") var shouldUpdate = true @AppStorage("shouldLoad") var shouldLoad = true init(){ if shouldLoad{ print("Loading...") shouldUpdate = true // prepare for next update shouldLoad = false }else{ print("No need to relaod...") } } } App version 4 - UPDATE 3. Repeat what I did in UPDATE 1...
7
0
1.5k
Jan ’23
Group Core Data items by category in List in SwiftUI
I have a Core Data container with two entities, a Category and an Item. The Item can have one Category assigned and the Category can be assigned to many Items. What I need to do is group the items by category in a list in SwiftUI. The code below doesn't group all items by category, it only shows one item by category. How can I group all items that have the same category assigned under the same category group? Core Data Entities Category Attributes name Relationship items (Type: To Many) Item Attributes name Relationship category (Type: To One) Swiftui struct ItemsView: View { let selectedList:List @EnvironmentObject private var itemSM: ItemServiceModel var body: some View { List { ForEach(itemSM.items) { item in Section(header: Text(item.category?.name ?? "")) { ForEach(itemSM.items.filter { $0.category == item.category }) { filteredItem in Text("\(filteredItem.name ?? "")") } } } } .onAppear{ itemSM.loadItems(forList: selectedList) } } } Service Item Service Model class ItemServiceModel: ObservableObject{ let manager: CoreDataManager @Published var items: [Item] = [] func loadItems(forList list: List){ let request = NSFetchRequest<Item>(entityName: "Item") let sort = NSSortDescriptor(keyPath: \Item.name, ascending: true) request.sortDescriptors = [sort] let filter = NSPredicate(format: "list == %@", list) request.predicate = filter do{ items = try manager.context.fetch(request) }catch let error{ print("Error fetching items. \(error.localizedDescription)") } } } This is what I see, as you can see, only one Fruits & Vegetables section should exist.
4
0
1.3k
Sep ’23
How to resize the image displayed when selecting an item in a Picker view
Is there a way to remove or resize the image from the tag in the Picker view? Picker("", selection: $selectedCategory) { ForEach(categorySM.categories, id: \.self) { category in HStack { if let inputImage = UIImage(data: category.image ?? Data()) { Image(uiImage: inputImage) .resizable() .scaledToFit() } Text(category.name ?? "") } .tag(category as CategoryItem?) } } .font(.callout) .pickerStyle(.menu) As you can see in images 1 and 2 below, the image in the tag from the Beverages category is huge and covers almost the entire screen, it also covers the category name (Beverages). Is there a way to remove or resize the image when displaying it on the tag? Basically to make it look like image #3. Image Link: https://i.stack.imgur.com/4XpjI.jpg
4
0
1.6k
Sep ’23
Can the NSPersistentCloudKitContainer mirror the data from the cloudKit public database to the local Core Data if the user is not logged in?
I'm currently syncing Core Data with the CloudKit public database using NSPersistentCloudKitContainer. The app starts with an empty Core Data store locally and at the app launch it downloads the data from CloudKit public database to the Core Data store, but this can only be accomplished if the user is logged in, if the user is not logged, no data gets downloaded and I get the error below. Can the NSPersistentCloudKitContainer mirror the data from the CloudKit public database to the local Core Data even if the user is not logged in? Can someone please confirm this is possible? The reason for my question is because I was under the impression that the users didn't need to be logged to read data from the public database in CloudKit but I'm not sure this applies to NSPersistentCloudKitContainer when mirroring data. I know I can fetch data directly with CloudKit APIs without the user beign logged but I need to understand if NSPersistentCloudKitContainer should in theory work without the user being logged. I hope someone from Apple sees this question since I have spent too much time researching without any luck. Error Error fetching user record ID: <CKError 0x600000cb1b00: "Not Authenticated" (9); "No iCloud account is configured"> CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1192): <NSCloudKitMirroringDelegate: 0x600003b00460>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x10700f0d0> (URL: file:///Users/UserName/...
4
0
973
Jun ’24
Rename or find where a custom Color Asset has been used in Xcode
Now with Dark Mode I have been creating color assets to manage dark mode colors, but sometimes I think of a more semantic name for a certain color after I have used it in multiple places and I was wondering if there is a way to find where a color has been used even if I have to go and manually changed them, I'm not really looking for an automatic solution.Is there a way to rename or find all of the instances where a custom color asset has been used in Xcode?Thanks!
6
0
6.2k
Apr ’21
Why do I get an error when deleting data from SwiftUI List and Realm?
Hi all,I know this is may not be the right forum to post questions about Realm but my issue has to do with the nature of how SwiftUI works.Has anyone been able to successfully integrate Realm with SwiftUI, especially deleting records/rows from a SwiftUI List? I have tried a few different things but no matter what I do I get the same error. After reading some related threads I found out that other people have the same issue but I refuse to think that there is no current solution to integrate Realm with SwiftUI.The following code successfully presents all of the items from Realm in a SwiftUI List, I can create new ones and they show up in the List as expected, my issues is when I try to delete records from the List by either manually pressing a button or by left-swiping to delete the selected row, I get an Index is out of bounds error.Here is my code.Realm Model:class Dog: Object { @objc dynamic var name = "" @objc dynamic var age = 0 @objc dynamic var createdAt = NSDate() @objc dynamic var userID = UUID().uuidString override static func primaryKey() -&gt; String? { return "userID" } }SwiftUI Code:class BindableResults: ObservableObject where Element: RealmSwift.RealmCollectionValue { var results: Results private var token: NotificationToken! init(results: Results) { self.results = results lateInit() } func lateInit() { token = results.observe { [weak self] _ in self?.objectWillChange.send() } } deinit { token.invalidate() } } struct DogRow: View { var dog = Dog() var body: some View { HStack { Text(dog.name) Text("\(dog.age)") } } } struct ContentView : View { @ObservedObject var dogs = BindableResults(results: try! Realm().objects(Dog.self)) var body: some View { VStack{ List{ ForEach(dogs.results, id: \.name) { dog in DogRow(dog: dog) }.onDelete(perform: deleteRow ) } Button(action: { try! realm.write { realm.delete(self.dogs.results[0]) } }){ Text("Delete User") } } } private func deleteRow(with indexSet: IndexSet){ indexSet.forEach ({ index in try! realm.write { realm.delete(self.dogs.results[index]) } }) } }ERRORTerminating app due to uncaught exception ‘RLMException’, reason: ‘Index 23 is out of bounds (must be less than 23).’Of course, the 23 changes depending on how many items are in the Realm database, in this case, I had 24 records when I swiped and tapped the delete button.
4
0
3.7k
Apr ’22
CloudKit sync works when testing on devices in Xcode but not in production/App Store
I have a SwiftUI app that uses CloudKit and Core data to sync data between devices. Everything works fine when testing on devices in Xcode but not in production in the App Store. Can someone explain the typical process when deploying an app that uses CoreData + CloudKit? Is there anything that needs to be done in code or in the CloudKit Console before the app is uploaded to the App Store? Again, my issue is that data doesn't sync when trying to sync data between multiple devices in production but works fine when testing in Xcode. Thanks
3
1
1.4k
Jan ’23
CloudKit Stopped Syncing after adding new Entities
Can someone please shed some light? I have an app that uses Core Data and CloudKit, up until the last version, I was able to sync data between devices but now after I added two new Entities for some reason it stopped syncing between devices. Here is how I did the change: Created a new Core Data container. Added the new Entities and their Attributes Tested the new Entities locally to be able to send the new schema to CloudKit. Went to CloudKit and made sure that the new Entities and Attributes were reflected on the Developent database. Deploy Schema Cahnges. Went to the Production database to make sure the new schema was deployed; and it was, both databases look the same. Testing: Tested in the simulator and with a real device and everything syncs, even the new Entities. If I download the app from the App Store on two different devices they do NOT sync. Based on the procedure I'm describing above, is there any important step I may have missed when doing the migration? I'm not sure if this is related to the syncing issue but after testing a few times, I no longer can turn the iCloud on, I get the following message when I try to turn iCloud Sync On. CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate resetAfterError:andKeepContainer:]: <NSCloudKitMirroringDelegate: 0x282c488c0> - resetting internal state after error: Error Domain=NSCocoaErrorDomain Code=134410 "CloudKit setup failed because there is another instance of this persistent store actively syncing with CloudKit in this process." UserInfo={NSURL=file:///var/mobile/Containers/Data/Application/73F19BC7-4538-4098-85C7-484B36192CF3/Library/Application%20Support/CoreDataContainer.sqlite, NSLocalizedFailureReason=CloudKit setup failed because there is another instance of this persistent store actively syncing with CloudKit in this process., NSUnderlyingException=Illegal attempt to register a second handler for activity identifier com.apple.coredata.cloudkit.activity.setup.8D4C04F6-8040-445A-9447-E5646484521} Any idea of what could be wrong and preventing the devices from syncing? Any idea or suggestion is welcome. Thanks
3
0
3.2k
Jun ’24
Why the first item I tap on a SwiftUI List becomes nil when present it in a sheet
Can someone please explain why the first time I tap on an item from the list, the selectedItem?.name becomes nil? The second time it shows the right item name correctly but not the first time, why?. I was expecting that to have a value even the first time since I'm setting it in the onTapGesture method selectedItem = item. // model struct Item: Identifiable{   var id = UUID()   var name:String } // SwiftUI struct UpdateStateProperty: View {   var items:[Item] = [Item(name: "Oranges"),             Item(name: "Apples"),             Item(name: "Cookies") ]   @State private var presentView = false   @State private var selectedItem: Item?   var body: some View {     List{       ForEach(items){ item in         HStack{           Text(item.name)         }.onTapGesture {           selectedItem = item           presentView.toggle()         }       }     }     .sheet(isPresented: $presentView){       Text("\(selectedItem?.name)" as String)     }   } }
2
1
2.8k
Feb ’22
How to turn On/Off iCloudKitSync on a SwiftUI application
I'm trying to give the user the ability to decide whether they want to sync to CloudKit or not by turning On or Off a Switch located somewhere in the app settings screen but I'm not sure how to do it in SwiftUI. The following code successfully stops the sync to CloudKit by setting the cloud kit container options to nil description.cloudKitContainerOptions = nil. class CoreDataManager{ static let instance = CoreDataManager() let container: NSPersistentCloudKitContainer let context: NSManagedObjectContext init(){ container = NSPersistentCloudKitContainer(name: "CoreDataContainer") guard let description = container.persistentStoreDescriptions.first else{ fatalError("###\(#function): Failed to retrieve a persistent store description.") } description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) description.cloudKitContainerOptions = nil container.loadPersistentStores { (description, error) in if let error = error{ print("Error loading Core Data. \(error)") } } container.viewContext.automaticallyMergesChangesFromParent = true context = container.viewContext } func save(){ do{ try context.save() print("Saved successfully!") }catch let error{ print("Error saving Core Data. \(error.localizedDescription)") } } } What I ultimately want is to be able to control the syncing process with an @AppStore property or some sort of property, something like this... class CoreDataManager{ @AppStorage("iCloudSync") private var iCloudSync = false //missing code... init(){ if !iCloudSync{ description.cloudKitContainerOptions = nil } } //missing code... } But I'm facing a couple of issues, one, I'm getting an error when using the iCloudSync wrapper variable and the second one and the most difficult for me to solve is the fact that I need to make the persistent storage reload when the switch changes from On to Off of vise-versa. Any idea how can I structure my code so I can control the syncing process and be able to reload the persistent storage when the switch changes? By the way and just for reference, here is how I'm using the CoreDataManager class in my view model. class CarViewModel: ObservableObject{ let manager = CoreDataManager.instance @Published var cars: [Car] = [] init(){ getCars() } func addCar(){} func getCars(){} func deleteCar(){} func save(){ self.manager.save() } }
2
0
1.5k
Jan ’22
What's the recommended code structure to manage apps with MVVM+SwiftUI+CoreData
I have a SwiftUI app with 3 Core Data entities, Car, CarService and ServiceRecord where Car has many carServices and each CarService has many serviceRecords. Everything is working fine but I'm not sure what's the most common MVVM practice. As you can see in the following example I'm using CoreDataViewModel to fetch data from Core Data and then I pass it around all SwiftUI views, at the moment I don't have a ViewModel for the view's logic (the logic is currently inside of each view) but that's something I would like to incorporate, but I'm not sure what would be the best way to do that. I'm thinking about the two following option... Create a view model for each view (CarViewModel, ServicesViewModel and RecordsViewModel) to handle ONLY the view's logic and leave the existing CoreDataViewModel as is. Create a view model for each view, CarViewModel, ServicesViewModel and RecordsViewModel to handle the logic for each view and move the CoreData quests to each of the view models respectably and basically delete CoreDataViewModel altogether since now the core data related will live inside each view model. Which of the two options above makes more sense for an MVVM app? In general, can someone please share how you usually structure your code when using MVVM + CoreData + SwiftUI? CoreDataManager class CoreDataManager{ static let instance = CoreDataManager() lazy var context: NSManagedObjectContext = { return container.viewContext }() lazy var container: NSPersistentContainer = { return setupContainer() }() func setupContainer()->NSPersistentContainer{ // code to setup container... return container } func save(){ do{ try context.save() }catch let error{ print("Error saving Core Data. \(error.localizedDescription)") } } } CoreDataViewModel class CoreDataViewModel: ObservableObject{ let manager: CoreDataManager @Published var cars: [Car] = [] @Published var carServices: [CarService] = [] @Published var serviceRecords: [ServiceRecord] = [] init(coreDataManager: CoreDataManager = .instance){ self.manager = coreDataManager // getCars() etc. } // CREATIONS func addCar(name:String){} func addService(name:String, cost: Double){} func createRecord(name:String, cost: Double){} // DELETES func deleteCar(){} func deleteCarService(){} func deleteServiceRecord(){} // UPDATES func updateCar(){} func updateService(){} // GETS func getCars(){} func getServices(){} func getRecords(){} func save(){ self.manager.save() } } SwiftUI Views CarsView struct CarsView: View { @StateObject var coreDataViewModel = CoreDataViewModel() var body: some View { NavigationView{ VStack{ List { ForEach(coreDataViewModel.cars) { car in } } } } } } ServicesView struct ServicesView: View { @ObservedObject var coreDataViewModel:CoreDataViewModel var body: some View { NavigationView{ VStack{ List { ForEach(coreDataViewModel.carServices) { service in } } } } } } RecordsView struct RecordsView: View { @ObservedObject var coreDataViewModel: CoreDataViewModel var body: some View { NavigationView{ VStack{ List { ForEach(coreDataViewModel.serviceRecords) { record in } } } } } } Thanks!
2
1
3.7k
May ’22
How to update the CloudKit schema after the app has been released to the AppStore
Hi, I recently had an issue with one of my production apps that use Core Data and CloudKit where data wasn't syncing between devices, after a little bit of research I found out that the schema in the private CloudKit container needed to be initialized; which I never did. The part I'm still not 100% sure is when to run the initializeCloudKitSchema method after the app has been released to the AppStore. I see that Apple recommends running it when testing by using #if DEBUG, but... do you really want to run it every time you compile in Xcode? Here is how I understand it at this point... App release, call initializeCloudKitSchema() to match schemas between Core Data and CloudKit. Added or deleted an attribute, call initializeCloudKitSchema() to update the CloudKit schema. Renamed an attribute, call initializeCloudKitSchema() to update the CloudKit schema. Etc. If my assumption above is correct, calling the initializeCloudKitSchema() method during development would update the schema in CloudKit before the new app version is released in the AppStore, therefore creating an issue for existing users with previous versions of the app since they will not have the latest code but will be using the latest schema which contains the new attributes. Can someone please share their method of handling schema updates in CloudKit after the app has been released to the AppStore? Code: do { try container.initializeCloudKitSchema() } catch { print(error) }
2
0
2.0k
Jun ’22
Understanding Dependency Injection - Swift
Hi, I would like to have a better understanding of Dependency Injection, in general, to start using it. Based on the three examples below, can someone please point out what would be the pros and the cons of using one over the other? 1 - What problem could Example 1 cause if I don't do unitest? This is my current method. 2- What method of Dependency Injection is best to adopt, Example 2 ro Example 3? 3- I noticed that Example 2 enforces to provided the dependency object at instantiation time whereas Example 3 does not. Couldn't Example 3 create confusion for the programmer since if you don't provide the dependency object, the code will still compile without any warning if you call a function that relies on a method inside the injected object? Example 1: Without Dependency Injection class Stereo{ func volume(){ print("Adjusting volume...") } } class Car{ var stereo = Stereo() func adjustVolume(){ stereo.volume() } } let car = Car() car.adjustVolume() Example 2: With Dependency Injection class Stereo{ func volume(){ print("Adjusting volume...") } } class Car{ var stereo: Stereo init(stereo: Stereo){ self.stereo = stereo } func adjustVolume(){ stereo.volume() } } let car = Car(stereo: Stereo()) car.adjustVolume() Example 3: With Optional Dependency Injection class Stereo{ func volume(){ print("Adjusting volume...") } } class Car{ var stereo: Stereo? func adjustVolume(){ stereo?.volume() } // this method can be called without injecting Stereo func someOtherFunction(){ print("Calling some other function...") } } let car = Car() car.stereo = Stereo() car.adjustVolume() Thanks
2
0
2.1k
Jan ’23
How to upload default-data to the public CloudKit database
I need some help understanding how the public database works in CloudKit. First of all, let me say that I know how to connect and use the private database. In this question I'm not looking for an answer on how to connect to the database at self, only the concept. Here is my confusion. I have a set of images that all users in my app will be using, right now what I'm doing is adding the images directly to the app (an Image Set in Xcode) and then I am pulling them to Core Data and then syncing them to CloudKit. As you can see all images are technically stored in every device using my app and in Core Data/CloudKit, not very efficient. What I would like is to have the images stored in a single place where all uses can pull the images from, in this case CloudKit. I know I can have them somewhere in a private server, but I feel like I would be adding more complexity to my app, so I think using CloudKit is a better option for me. Here is my question. How do I get the images to CloudKit, do I upload them directly to CloudKit and then read from all devices or do I need to first add them to a device and upload to CloudKit from there? Thanks!
2
0
905
Sep ’23