Post

Replies

Boosts

Views

Activity

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
631
May ’22
UIImage causes memory to run out
I have a project that currently has data saved locally and I'm trying to get it to sync over multiple devices. Currently basic data is syncing perfectly fine, but I'm having issues getting the images to convert to data. From what I've researched it because I'm using a UIImage to convert and this caches the image It works fine when there's only a few images, but if there's several its a pain The associated code func updateLocalImages() { autoreleasepool { let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest() fetchRequest.predicate = NSPredicate(format: "converted = %d", false) fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Project.statusOrder?.sortOrder, ascending: true), NSSortDescriptor(keyPath: \Project.name, ascending: true)] do { let projects = try viewContext.fetch(fetchRequest) for project in projects { currentPicNumber = 0 currentProjectName = project.name ?? "Error loading project" if let pictures = project.pictures { projectPicNumber = pictures.count for pic in pictures { currentPicNumber = currentPicNumber + 1 let picture : Picture = pic as! Picture if let imgData = convertImage(picture: picture) { picture.pictureData = imgData } } project.converted = true saveContext() } } } catch { print("Fetch Failed") } } } func convertImage(picture : Picture)-> Data? { let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let path = paths[0] if let picName = picture.pictureName { let imagePath = path.appendingPathComponent(picName) if let uiImage = UIImage(contentsOfFile: imagePath.path) { if let imageData = uiImage.jpegData(compressionQuality: 0.5) { return imageData } } } return nil }```
3
0
1k
Jan ’25
EXC_BAD_ACCESS When saving core data
I'm trying to convert some data, then save it back to Core Data. Sometimes this works fine without an issue, but occasionally I'll get an error Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) It seems to occur when saving the core data context. I'm having trouble trying to debug it as it doesn't happen on the same object each time and can't reliably recreate the error Full view code can be found https://pastebin.com/d974V5Si but main functions below var body: some View { VStack { // Visual code here } .onAppear() { DispatchQueue.global(qos: .background).async { while (getHowManyProjectsToUpdate() > 0) { leftToUpdate = getHowManyProjectsToUpdate() updateLocal() } if getHowManyProjectsToUpdate() == 0 { while (getNumberOfFilesInDocumentsDirectory() > 0) { deleteImagesFromDocumentsDirectory() } if getNumberOfFilesInDocumentsDirectory() == 0 { DispatchQueue.main.asyncAfter(deadline: .now()) { withAnimation { self.isActive = true } } } } } } } update local function func updateLocal() { autoreleasepool { let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest() fetchRequest.predicate = NSPredicate(format: "converted = %d", false) fetchRequest.fetchLimit = 1 fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Project.name, ascending: true), NSSortDescriptor(keyPath: \Project.name, ascending: true)] do { let projects = try viewContext.fetch(fetchRequest) for project in projects { currentPicNumber = 0 currentProjectName = project.name ?? "Error loading project" if let projectMain = project.mainPicture { currentProjectImage = getUIImage(picture: projectMain) } if let pictures = project.pictures { projectPicNumber = pictures.count // Get main image if let projectMain = project.mainPicture { if let imgThumbData = convertImageThumb(picture: projectMain) { project.mainPictureData = imgThumbData } } while (getTotalImagesToConvertForProject(project: project ) > 0) { convertImageBatch(project: project) } project.converted = true saveContext() viewContext.refreshAllObjects() } } } catch { print("Fetch Failed") } } } convertImageBatch function func convertImageBatch(project: Project) { autoreleasepool { let fetchRequestPic: NSFetchRequest<Picture> = Picture.fetchRequest() let projectPredicate = NSPredicate(format: "project = %@", project) let dataPredicate = NSPredicate(format: "pictureData == NULL") fetchRequestPic.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [projectPredicate, dataPredicate]) fetchRequestPic.fetchLimit = 5 fetchRequestPic.sortDescriptors = [NSSortDescriptor(keyPath: \Picture.dateTaken, ascending: true)] do { let pictures = try viewContext.fetch(fetchRequestPic) for picture in pictures { currentPicNumber = currentPicNumber + 1 if let imgData = convertImage(picture: picture), let imgThumbData = convertImageThumb(picture: picture) { // Save Converted picture.pictureData = imgData picture.pictureThumbnailData = imgThumbData // Save Image saveContext() viewContext.refreshAllObjects() } else { viewContext.delete(picture) saveContext() viewContext.refreshAllObjects() } } } catch { print("Fetch Failed") } } } And finally saving func saveContext() { do { try viewContext.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } }
6
0
202
Jul ’25
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
945
Apr ’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
468
Oct ’23
Setting up Query in init causes app to freeze
I'm trying to filter results to get all characters that are associated with a story. When navigating to the view it filters as it should, but as soon as you select the Add Character option, the app just freezes. I've found that if I comment out the Query line in the init, and filter out as part of the foreach I don't get the freeze. Can anyone advise how I should be doing these predicates as it seems init isn't the best place to do it The views code is import SwiftUI import SwiftData struct CharacterListView: View { let story : Story @Query(sort: \Character.name, order: .forward) private var characters: [Character] var body: some View { List { ForEach(characters) { char in CharacterListCellView(char: char) } } .toolbar { ToolbarItem { NavigationLink(destination: CharacterAddView(story: story)) { Label("Add Character", systemImage: "plus") } } } .navigationBarTitle("Characters", displayMode: .inline) } init(story: Story) { self.story = story let id = story.persistentModelID let predicate = #Predicate<Character> { char in char.story?.persistentModelID == id } _characters = Query(filter: predicate, sort: [SortDescriptor(\.name)] ) } } I've narrowed it down to a line in the AddCharacterView file struct CharacterAddView: View { @Environment(\.modelContext) private var modelContext @Environment(\.dismiss) var dismiss let story : Story // Character details @State private var characterName: String = "" // Images @State private var showImageMenu = false; @State private var isShowPhotoLibrary = false @State private var isShowCamera = false //@State private var image = UIImage() <-- This line @State private var imageChanged = false @State private var isSaving : Bool = false var body: some View { } } If I do either of the below it works If I comment out the UIImage variable then I don't get the freeze, but I can't update the image variable If I comment out the query in init, then it works but doesn't filter and I have to do it as part of the foreach
1
1
570
May ’24
Download & replace app container
I used to download and replace the app container when I was testing, essentially downloading the container from the live app, and restoring it into the test app in order to not affect the live app, but to test major changes on "live" data. it seems the option for downloading and replacing in Xcode no longer works, I will sometimes get a container downloaded, other times it only part downloads. I can never seem to get it to replace. No errors, but it doesn't work on the new device. It used to be that devices & simulators showed when it was downloading and replacing but it no longer does that. Is there another way of doing this? Currently I'm having to take a backup of the live phone, restore on the test device, then delete all the unneeded apps, otherwise the restore takes ages, then backup the test device and restore every time I need to restart.
0
1
274
Nov ’24
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.2k
May ’21