Post

Replies

Boosts

Views

Activity

Reply to Using NSSortDescriptor(key: "date", ascending: true
Drew, some quick comments, since i cannot see your code (if it were available on GitHub, that might be of interest). in the Core Data model editor, find the attribute date in whatever entity you are working with and check that its Type shows as Date. if it shows as String there, just click on the String value and choose Date as the Type from the pull-down menu. (you may already have done this, but i cannot tell from your question.) so Core Data knows the values are dates, and sorting with ascending: false will give most recent dates first, oldest dates last. Xcode will give your app/program access to the date attribute as a Swift variable var date: Date? defined on the class object for your entity. notice that this is an optional Date in Swift, so treat it accordingly when reading it. all your existing code that reads from the date attribute or assigns to the date attribute should be working with real Date objects in Swift. you cannot assign a String to a Date? object; but really, other than for display purposes, you should not be working with string values of dates in your code anyway, since constantly translating back and forth between Strings and Dates is hard. hope that helps, DMG
Topic: App & System Services SubTopic: iCloud Tags:
Mar ’22
Reply to Using NSSortDescriptor(key: "date", ascending: true
hi, you do not show the definition of your Core Data entity, nor the type of its attribute "date." since the list of dates appears to be sorted alphabetically, i am guessing that you defined the date attribute in Core Data as a String (not as a Date) and that you are storing strings in what you call "date." that would explain the result above. (BTW: to have Core Data sort by the Date type, you would want "ascending: false" for the NSSortDescriptor so that the most recent date comes out first.) hope that helps, DMG
Topic: App & System Services SubTopic: iCloud Tags:
Feb ’22
Reply to NSPersistentCloudKitContainer exclude relationship from share
hi, ... this may not directly answer your CloudKit sharing, but what you may be asking is how to place all Core Data Folder and Comment objects into a local persistent store configuration, and allItem objects into a cloud configuration that is shared with iCloud. if that's the case, you cannot implement direct relationships between configurations, so then you might: in every Item, store the id: UUID of the Folder it belongs to (you do put UUIDs into your Core Data entities, yes?) ... sort of a parentID, and in every Comment, store the id: UUID of the Item it belongs to, again, essentially a parentID. you can then finesse the relationships, say for an Item, finding its parent Folder by doing a Core Data fetch for Folder objects whose id matches the Item's parentID; and similarly, you can locate the Comments associated with an Item by doing a Core Data fetch for Comment objects whose parentID matches the Item's id. Also take a look at this Apple Sample project on how to handle separate stores in Core Data. hope that helps, DMG
Topic: App & System Services SubTopic: iCloud Tags:
Feb ’22
Reply to How to increase or decrease the probability of an item being picked from an array?
hi, the easiest, fastest way might just be to change the definition of numbers, so that the elements appears with the frequencies you like. since we're talking 1/20-ths here (multiples of 5%), give the array 20 elements: let numbers = ["1", "1", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "4", "4", "5"] if let pickedNumber = numbers.randomElement() { print(pickedNumber) } there are more general ways to do this, but keep it simple for now .. hope that helps, DMG
Feb ’22
Reply to Separating CoreData functionality from the View (SwiftUI) but Still with observability
hi, your ViewModel simply needs to use an NSFetchedResultsController to hook into Core Data, and keep the array of Core Data objects that you would ordinarily define in the View (using @FetchRequest) as a @Published property. whenever the resultsController fires, update the array property. be sure your View keeps a reference to the ViewModel as an @ObservedObject. hope that helps, DMG
Topic: App & System Services SubTopic: iCloud Tags:
Jan ’22
Reply to @FetchRequest in a class, SwiftUI
hi, you can certainly do a fetch directly with core data; but if you expect your class object to find out about changes in Core Data and act like you had a @FetchRequest, you would want to use an NSFetchedResultsController in your object to see changes over time. hope that helps, DMG
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Using NSSortDescriptor(key: "date", ascending: true
Drew, some quick comments, since i cannot see your code (if it were available on GitHub, that might be of interest). in the Core Data model editor, find the attribute date in whatever entity you are working with and check that its Type shows as Date. if it shows as String there, just click on the String value and choose Date as the Type from the pull-down menu. (you may already have done this, but i cannot tell from your question.) so Core Data knows the values are dates, and sorting with ascending: false will give most recent dates first, oldest dates last. Xcode will give your app/program access to the date attribute as a Swift variable var date: Date? defined on the class object for your entity. notice that this is an optional Date in Swift, so treat it accordingly when reading it. all your existing code that reads from the date attribute or assigns to the date attribute should be working with real Date objects in Swift. you cannot assign a String to a Date? object; but really, other than for display purposes, you should not be working with string values of dates in your code anyway, since constantly translating back and forth between Strings and Dates is hard. hope that helps, DMG
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Mar ’22
Reply to Using NSSortDescriptor(key: "date", ascending: true
hi, you do not show the definition of your Core Data entity, nor the type of its attribute "date." since the list of dates appears to be sorted alphabetically, i am guessing that you defined the date attribute in Core Data as a String (not as a Date) and that you are storing strings in what you call "date." that would explain the result above. (BTW: to have Core Data sort by the Date type, you would want "ascending: false" for the NSSortDescriptor so that the most recent date comes out first.) hope that helps, DMG
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Feb ’22
Reply to NSPersistentCloudKitContainer exclude relationship from share
hi, ... this may not directly answer your CloudKit sharing, but what you may be asking is how to place all Core Data Folder and Comment objects into a local persistent store configuration, and allItem objects into a cloud configuration that is shared with iCloud. if that's the case, you cannot implement direct relationships between configurations, so then you might: in every Item, store the id: UUID of the Folder it belongs to (you do put UUIDs into your Core Data entities, yes?) ... sort of a parentID, and in every Comment, store the id: UUID of the Item it belongs to, again, essentially a parentID. you can then finesse the relationships, say for an Item, finding its parent Folder by doing a Core Data fetch for Folder objects whose id matches the Item's parentID; and similarly, you can locate the Comments associated with an Item by doing a Core Data fetch for Comment objects whose parentID matches the Item's id. Also take a look at this Apple Sample project on how to handle separate stores in Core Data. hope that helps, DMG
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Feb ’22
Reply to How to increase or decrease the probability of an item being picked from an array?
hi, the easiest, fastest way might just be to change the definition of numbers, so that the elements appears with the frequencies you like. since we're talking 1/20-ths here (multiples of 5%), give the array 20 elements: let numbers = ["1", "1", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "4", "4", "5"] if let pickedNumber = numbers.randomElement() { print(pickedNumber) } there are more general ways to do this, but keep it simple for now .. hope that helps, DMG
Replies
Boosts
Views
Activity
Feb ’22
Reply to Separating CoreData functionality from the View (SwiftUI) but Still with observability
hi, your ViewModel simply needs to use an NSFetchedResultsController to hook into Core Data, and keep the array of Core Data objects that you would ordinarily define in the View (using @FetchRequest) as a @Published property. whenever the resultsController fires, update the array property. be sure your View keeps a reference to the ViewModel as an @ObservedObject. hope that helps, DMG
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to Core Data Predicate with UUID attribute
hi, why not simply write the predicate as NSPredicate(format: "clientId == %@", clientId! as CVarArg) // i would think the force unwrap would help here ? i have used this; it works. hope that helps, DMG
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to @FetchRequest in a class, SwiftUI
hi, you can certainly do a fetch directly with core data; but if you expect your class object to find out about changes in Core Data and act like you had a @FetchRequest, you would want to use an NSFetchedResultsController in your object to see changes over time. hope that helps, DMG
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’21