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
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
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.
I'm trying to move an app over to swiftui and have run into a couple of issues involving quick actions.
I'm trying to get the app to respond to the action.
I have a CustomSceneDelegate setup, which is receiving the actions.
How do I get it to navigate to a view when the addProject is used, and how do I get a core data query in the addTime?
class CustomSceneDelegate: UIResponder, UIWindowSceneDelegate {
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
handleShortcutItem(shortcutItem)
}
func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
if shortcutItem.type == "addProject" {
print("Phase: Add Project")
} else if shortcutItem.type == "addTime" {
print("Phase: Add Time")
}
}
}
I'm trying to setup a filter option for some core data records, but I'm not seeing the expected results.
Essentially, what I want is to have multiple sections. Within each section it would be an OR query, and an AND between each section. I've tried setting it up as below.
func getBasicFilter() -> NSPredicate? {
var predicatesBagsLeft : [NSPredicate] = []
var predicatesDrillType : [NSPredicate] = []
var andPredicates : [NSPredicate] = []
print("Filter List")
print(filters)
// Bags Left
if let show = filters["showFullLeft"] {
if show {
print("Predicates : Show Full")
let predicate = NSPredicate(format: "ANY projectColours.fullLeft > 0")
predicatesBagsLeft.append(predicate)
}
}
if let show = filters["showPartialLeft"] {
if show {
print("Predicates : Show Partial")
let predicate = NSPredicate(format: "ANY projectColours.partialLeft > 0")
predicatesBagsLeft.append(predicate)
}
}
// Drill Types
if let show = filters["showSquareOnly"] {
if show {
print("Predicates : Show Square Only")
let predicate = NSPredicate(format: "ANY projectColours.project.drillType = %@", "Square")
predicatesDrillType.append(predicate)
}
}
// Drill Manufacturers - TO DO
// Combine Predicates
if predicatesBagsLeft.count > 0 {
let predicatesForBagsLeft = NSCompoundPredicate(type: .or, subpredicates: predicatesBagsLeft)
andPredicates.append(predicatesForBagsLeft)
}
if predicatesDrillType.count > 0 {
let predicatesForDrillType = NSCompoundPredicate(type: .or, subpredicates: predicatesDrillType)
andPredicates.append(predicatesForDrillType)
}
if andPredicates.count > 0 {
let predicates = NSCompoundPredicate(type: .and, subpredicates: andPredicates)
return predicates
}
return nil
}
It does filter, but doesn't seem to be applying both correctly. I'm testing with a filter of showFullLeft & showSquareOnly, so it should show only squares which have a a fullest > 0
I'm getting 7 results back, but one of them is unwanted. It is square, but it has 0 for both full & partial
When I look at the query core data is using it looks correct
CoreData: sql: SELECT t0.ZMANU, COUNT (DISTINCT t0.Z_PK) FROM ZCOLOUR t0 JOIN ZPROJECTCOLOUR t1 ON t0.Z_PK = t1.ZCOLOUR JOIN ZPROJECTCOLOUR t2 ON t0.Z_PK = t2.ZCOLOUR JOIN ZPROJECT t3 ON t2.ZPROJECT = t3.Z_PK WHERE ( t1.ZFULLLEFT > ? AND t3.ZDRILLTYPE = ?) GROUP BY t0.ZMANU ORDER BY t0.ZMANU
CoreData: details: SQLite bind[0] = 0
CoreData: details: SQLite bind[1] = "Square"
I'm trying to look at what the best way to do thumbnails for images that are saved in core data, which are being synced across multiple devices.
I know I can save a lower quality version into core data, but I'm wondering if there's a better way of doing it.
I've come across quick look thumbnailing which looks like what I want, but I'm not sure if it can be adapted for core data as its using file paths, whereas the images are stored in a data type property in core data.
From what I can tell, I'd have to save the image locally, produce the thumbnail, then delete the local image
So I’m trying to setup an existing app to work with iCloud syncing. The syncing part seems to be working for the most part.
When the app is first installed it sets up some data locally.
Throughout the life of the app additional data is added through updates. There is a user default setup that stores the current data version, then compares it with the new version. If it’s newer, then loads the additional data.
The issue I’ve got is a user can delete the app and reinstall, or install on another device which has that data version as 0, prompting another import even though the data in the cloud is current version, resulting in duplicate data once the sync is done.
How can I persist that version data? I’ve seen NSUbiquitousKeyValueStore which seems to be a cloud based version of user defaults, but it says not to rely on it if it’s critical to app functions
Topic:
App & System Services
SubTopic:
iCloud & Data
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.
I have a grid setup where I'm displaying multiple images which is working fine. Images are ordered by the date they're added, newest to oldest.
I'm trying to set it up so that the user can change the sort order themselves but am having trouble getting the view to update.
I'm setting the fetch request using oldest to newest as default when initialising the view, then when its appears updating the sort descriptor
struct ProjectImagesListView: View {
@Environment(\.managedObjectContext) private var viewContext
var project : Project
let columns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
@FetchRequest var pictures: FetchedResults<Picture>
var body: some View {
ScrollView {
LazyVGrid(columns: columns) {
ForEach(pictures) { picture in
NavigationLink(destination: ProjectImageDetailView(picture: picture)) {
if let pictureData = picture.pictureThumbnailData, let uiImage = UIImage(data: pictureData) {
Image(uiImage: uiImage)
.resizable()
.scaledToFit()
.frame(height: 100)
} else {
Image("missing")
.resizable()
.scaledToFit()
.frame(height: 100)
}
}
}
}
}
.navigationBarTitle("\(project.name ?? "") Images", displayMode: .inline)
.onAppear() {
guard let sortOrder = getSettingForPhotoOrder() else { return }
guard let sortOrderValue = sortOrder.settingValue else { return }
NSLog("sortOrderPhotos: \(String(describing: sortOrder.settingValue))")
if sortOrderValue == "Newest" {
NSLog("sortOrderPhotos: Change from default")
let newSortDescriptor = NSSortDescriptor(keyPath: \Picture.dateTaken, ascending: false)
pictures.nsSortDescriptors = [newSortDescriptor]
}
}
}
func getSettingForPhotoOrder() -> Setting? {
let fetchRequest: NSFetchRequest<Setting> = Setting.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "name = %@", "photoSortOrder")
fetchRequest.fetchLimit = 1
do {
let results = try viewContext.fetch(fetchRequest)
return results.first
} catch {
print("Fetching Failed")
}
return nil
}
init(project: Project) {
self.project = project
_pictures = FetchRequest(
entity: Picture.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Picture.dateTaken, ascending: true)
],
predicate: NSPredicate(format: "project == %@", project)
)
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI