Post

Replies

Boosts

Views

Activity

Cloud Kit - Synching image
I'm currently sharing data between devices using core data and cloud kit.Currently I have a "Picture" entity which stores the filename, the date it was added and a relation to another entity.The details in core data successfully syncs, how would I go about syncing the image file. The file is saved in the documents folder and I can get its file path usinglet imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName)Research seems to show I need to use CKAsset to upload/download the file but how do I go about doing this?
3
0
2.1k
May ’20
Core Data Mapping Models
Is there an easier way in Xcode to create mapping models. Currently I'm on model 15 and having to create individual models from 1 to 15 is time consuming. I've tried copying an older model and changing the destination but am unable to see any of the new fields added
0
0
393
Aug ’20
Querying Cloud Kit Core Data Record
I have a app that I'm trying to migrate over to using CloudKit to store the core data so it can be synced across devices. The syncing is working fine, the issue I'm having is that the app adds some initial data to the database, this is causing it to be duplicated when its installed on another device. Currently I'm using user defaults to store a bool locally if the first run has been performed, which obviously only works per device Is there any way to see if data already exists in the cloud and prevent the addition of the initial data. Apple's documentation would suggest its possible if an object already exists but on first launch there's no objects in the local store. I don't want to query any particular record, just if records exist https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/reading_cloudkit_records_for_core_data
0
1
570
Sep ’20
Shortcut Items - Perform Segue
I'm trying to add a shortcut item to my app. I have the item appearing, and it's responding but can't work out how to get the segue to work. I have it going to the right tab but need to go to the root view and perform a segue from there. The segue is already setup on the ProjectList view controller and its called "addProject" My view storyboard is setup as so: UITabViewController -> UINavigationController-> UITableViewController (ProjectList) -> Other additional views func applicationDidBecomeActive(_ application: UIApplication) { 		if let shortcutItem = shortcutItemToProcess { 				if shortcutItem.type == "addProject" { 						if let window = self.window, let tabBar : UITabBarController = window.rootViewController as? UITabBarController { 								tabBar.selectedIndex = 0 								 						} 				} 				shortcutItemToProcess = nil 		} }
0
0
497
Oct ’20
Filtered list when going back from pushed view
I have the below code for my view's body Currently when I go to the ItemDetailView View and then press back to return to this view, the filtered list is no longer there, it's back the full view. How to I get it to keep the filtered list when I go back? The text is still in the search bar         NavigationLink(destination: ItemAddView(series: series), isActive: $noItemsShowing) { EmptyView() }         List {             ForEach(items) { item in                 NavigationLink(destination: ItemDetailView(item: item)) {                     Text(fullItemName(item: item))                 }             }         }         .searchable(text: $searchText)         .onSubmit(of: .search) {             items.nsPredicate = NSPredicate(format: "series = %@", series)             if(!searchText.isEmpty) {                 items.nsPredicate = NSPredicate(format: "series = %@ AND amount > 0 AND name CONTAINS[cd] %@", series, searchText)             }         }         .onChange(of: searchText) { _ in             items.nsPredicate = NSPredicate(format: "series = %@", series)             if(!searchText.isEmpty) {                 items.nsPredicate = NSPredicate(format: "series = %@ AND amount > 0 AND name CONTAINS[cd] %@", series, searchText)             }         }         .navigationBarTitle("\(series.name ?? "Error") Items", displayMode: .inline)     }
2
0
598
May ’22
Change List row background colour programatically
I'm trying to change the background colour of a row in a list based on a value I have an Int16 value stored in card.owned and I want to change the background if this value is above 0, otherwise use the normal background. Previously with a table view I could change it with the cellForRowAtIndexPath method, but not sure how to do it with SwiftUI without changing every row Currently I have ForEach(cards) { section in let header: String = nameForSectionHeader(sectionID: section.id) Section(header: Text(header)) { ForEach(section) { card in NavigationLink(destination: CardView(card: card)) { VStack(alignment: .leading) { if let name = card.name, let id = card.cardID { Text("\(id) - \(name)") } if let set = card.set, let setName = set.name { Text("Set: \(setName)") } if card.owned > 0 { Text("Owned: \(card.owned)") } } } } } } .listRowBackground(lightGreen) }
0
0
484
Sep ’22
Collapsible section headers
I have a list with sections headers on two views. One view has the section headers as collapsible, the other doesn't. Both use pretty much the same code, what defines if a section header is collapsible? Code for the one that doesn't collapse ForEach(cards) { section in let header: String = nameForSectionHeader(sectionID: section.id) Section(header: Text(header)) { ForEach(section) { card in NavigationLink(destination: CardView(card: card)) { HStack { Image(card.imageName ?? "") .renderingMode(.original) .resizable() .scaledToFit() .frame(width: 50.0) VStack(alignment: .leading) { if let name = card.name, let id = card.cardID { Text("\(id) - \(name)") } Text("Owned: \(card.owned)") } } } } } } } and the code for the one that does ForEach(cards) { section in let header: String = nameForSectionHeader(sectionID: section.id) Section(header: Text(header)) { ForEach(section) { card in NavigationLink(destination: CardView(card: card)) { VStack(alignment: .leading) { if let name = card.name, let id = card.cardID { Text("\(id) - \(name)") } if let set = card.set, let setName = set.name { Text("Set: \(setName)") } if card.owned > 0 { Text("Owned: \(card.owned)") } } } } } } .listRowBackground(lightGreen) }
1
0
1.4k
Sep ’22
Dynamically changing fetch request
I have the below fetch request which I want to be able to dynamically change the setup of. The request is setup initially as         sectionIdentifier: \.level,         sortDescriptors: [NSSortDescriptor(keyPath: \Card.level, ascending: true),                           NSSortDescriptor(keyPath: \Card.setID, ascending: true),                           NSSortDescriptor(keyPath: \Card.cardID, ascending: true),                           NSSortDescriptor(keyPath: \Card.name, ascending: true)],         animation: .default)     private var cards: SectionedFetchResults<Int16, Card> I've got the predicate change working fine, but get errors with the sort descriptors and the section identifier On setting the sort descriptors I get the error Cannot convert value of type 'NSSortDescriptor' to expected element type 'Array<SortDescriptor>.ArrayLiteralElement' (aka 'SortDescriptor') And setting the section identifier I get Cannot assign value of type 'SectionedFetchResults<String, Card>.Type' to type 'KeyPath<Card, Int16>' The code I have is below.                 if groupBy == 2 {                     cards.nsPredicate = nil                     let sortSetID = NSSortDescriptor(keyPath: \Card.setID, ascending: true)                     let sortName = NSSortDescriptor(keyPath: \Card.name, ascending: true)                     cards.sortDescriptors = [sortSetID, sortName] // This errors                     cards.sectionIdentifier = SectionedFetchResults<String, Card> // This errors                 }
1
0
891
Sep ’22
Check if data exists in cloud kit
My App does some importing when its first setup. I have it set to store core data in cloud kit. My issue is if you run it on one device, everything imports fine. Run it on a second, and it will import for a second time, duplicating the records. I'm trying to see if there's a way of checking if there is anything in cloud kit before running the import, but I'm always getting back false, even if I know there are records. The code I'm using to check is NSLog("Existance Check") checkIfExistsInCloudKit() { (result) in print(result) if result == false { NSLog("Does Not Exist") //let ImportData : ImportData = ImportData(viewContext: viewContext) //ImportData.importAll() } else { NSLog("Does Exist") } } NSLog("Existance Check Complete") and the function is func checkIfExistsInCloudKit(_ completion: @escaping (Bool) -> ()) { var result = false let container = CKContainer(identifier: "******") let privateDB = container.privateCloudDatabase let predicate = NSPredicate(format: "CD_brand = %@", "DMC") let query = CKQuery(recordType: "CD_Colour", predicate: predicate) privateDB.perform(query, inZoneWith: nil) { records, error in //guard let records = records else { return } result = true } completion(result) } 2023-04-15 14:07:28.092262+0100 Stitchers Companion[4553:113693] Existance Check false 2023-04-15 14:07:28.092897+0100 Stitchers Companion[4553:113693] Does Not Exist 2023-04-15 14:07:28.092962+0100 Stitchers Companion[4553:113693] Existance Check Complete
1
1
904
Apr ’23
Updating from Navigation view to NavigationStack
I'm currently using a navigationview and am now getting an warning 'init(destination:tag:selection:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:) inside a List within a NavigationStack or NavigationSplitView I'm trying to move to another view from a button (there will be several hence using tags) but I can't seem to get my head around how to do it. The code I currently have var body: some View { List { NavigationLink(destination: ProjectColourAddView(project: project), tag: 1, selection: $action) { EmptyView() } Group { Button(action: { self.showImageMenu = true }) { Text("New Title Image") } .confirmationDialog("Select Image Source", isPresented: $showImageMenu, titleVisibility: .visible) { Button("Take Photo") { self.isShowCamera = true } Button("Choose from Albums") { self.isShowPhotoLibrary = true } } Button(action: { self.action = 3 NSLog("More Images") }) { Text("More Images (x)") } Button(action: { self.action = 2 NSLog("Add Image") }) { Text("Add Image") } Button(action: { self.action = 1 NSLog("Colour List") }) { Text("Colour List") } } } }
2
0
635
Apr ’23
SwiftUI Cell Background colour - don't fill full cell
I'm looking to see if its possible to change the background colour of a cell, but fill it based on a percentage I'm creating the cell in a List {} let gradient = LinearGradient(colors: [.green, .white], startPoint: .leading, endPoint: .trailing) List { Section(header: Text("Total")) { Text("Total Obtained: \(totalObtained) - \(totalComplete)%") .listRowBackground(gradient) } } totalComplete has the percentage variable
0
0
506
Apr ’23
Xcode git keeps complaining of unsaved changes
Xcode keeps complaining about changes when I try and commit, I get the error "The local repository is out of date." When I try and pul changes from the remote, I get the message "Your local repository has uncommitted changes that need to be stashed before you can continue. " When I try and commit, every time it shows a UserInterfaceState.xcuserstate file. if I try and commit it I get the local repository error.
1
0
1.4k
Apr ’23
Changing property causes detail view to pop back to list view
I have a list view created from core data, and it's sectioned by a property called favourite. In the detail view a user is able to either add an item or remove it as a favourite. When the property is updated, it causes the detail view to pop back to the list view, which I don't want it to do, and don't know how to get it to not do that The creation of the list @SectionedFetchRequest var dims: SectionedFetchResults<Int16, DIM> init() { _dims = SectionedFetchRequest<Int16, DIM>( sectionIdentifier: \.favourite, sortDescriptors: [NSSortDescriptor(keyPath: \DIM.favourite, ascending: false), NSSortDescriptor(keyPath: \DIM.name, ascending: true)] ) } List { ForEach(dims) { section in let header: String = nameForSectionHeader(sectionID: section.id) Section(header: Text(header)) { ForEach(section) { dim in NavigationLink(destination: DIMListView(dim: dim)) { } } } } } and then on the detail view there's a toolbar with an option to add/remove favourite. .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button("Options") { showingAlertFav = true } .alert("Choose an option", isPresented: $showingAlertFav) { if dim.favourite == 0 { Button("Favourite", role: .none) { setFav(fav: 1) } } else { Button("Unfavourite", role: .none) { setFav(fav: 0) } } } } } func setFav(fav : Int16) { dim.favourite = fav saveContext() }
0
0
410
May ’23
List with multiple selections from core data
I'm trying to adjust a list so that it can have multiple selection. It works when I use hard coded data, but when I try and use it with core data, the option to edit the list doesn't bring the checkboxes up. Working code struct DIMTest: Identifiable { let id = UUID() let name: String } struct DimTypeListView: View { let contacts = [ DIMTest(name: "John"), DIMTest(name: "Alice"), DIMTest(name: "Bob") ] // 2 @State private var multiSelection = Set<DIM>() var body: some View { NavigationView { VStack { // 3 List(contacts, selection: $multiSelection) { contact in Text(contact.name) } Text("\(multiSelection.count) selections") } .navigationTitle("Contacts") .toolbar { // 4 EditButton() } } } } but when I adjust the code to the below to load from core data, I don't get the checkboxes @SectionedFetchRequest var dims: SectionedFetchResults<Bool, DIM> init(dType: DimType) { _dims = SectionedFetchRequest<Bool, DIM>( sectionIdentifier: \.favourite, sortDescriptors: [NSSortDescriptor(keyPath: \DIM.favourite, ascending: false), NSSortDescriptor(keyPath: \DIM.name, ascending: true)], predicate: NSPredicate(format: "dimType = %@", dType) ) } @State private var multiSelection = Set<DIM>() var body: some View { VStack { List(dims, selection: $multiSelection) { section in Section() { ForEach(section) { dim in Text("Name") } } } Text("\(multiSelection.count) selections") } .navigationTitle("DIMs") .toolbar { EditButton() } } It shows the correct number of entries, but can't be selected If i add an onDelete, i get the delete options but only want to be able to select, not delete
0
0
477
Jun ’23
SwiftData - Assignment randomly failing
I have a SwiftData class which has an attribute of var dimObject : DIM? I'm trying to assign a DIM object to that attribute, buts its randomly failing with Thread 1: EXC_BAD_ACCESS (code=1, address=0x8000000000000010) Its never the same object, nor do I get any further indications of what's causing it. I've narrowed it down to digimon.dimObject = dimFound in the below function func insertDigimon(digimon: Digimon) { Thread.sleep(forTimeInterval: 1) if let dimFound = getDIMFromID(dimID: digimon.dim) { print("****************************************************************") print("Digimon: \(digimon.name) - ID: \(digimon.id)") print("DIM: \(dimFound.name)") print("Set DIM Object on Digimon") digimon.dimObject = dimFound print("Digimon object set") print("Add digimon to DIM") dimFound.addToDigimon(d: digimon) print("Digimon added to DIM") } modelContext.insert(digimon) } If I comment out that line, there's no error Both the digimon and dimFound objects aren't nil and contain the expected object.
0
1
434
Oct ’23