I have
Pet
entities that can have multiple Events:public class Pet: NSManagedObject { @NSManaged var dbName: String @NSManaged var events: NSSet? // 1-to-many relationship with events } public class Event: NSManagedObject { @NSManaged var key: String @NSManaged var date: Int64 }
I've set Events to be indexed based on their date. I've made a function that returns all Events for a given pet.
func fetchEventsForPet(pet: String) -> [Event]? { let request = NSFetchRequest<NSFetchRequestResult>(entityName: Pet.entityName) let predicate = NSPredicate(format: "dbName == %@", pet) request.predicate = predicate request.fetchLimit = 1 do { let result = try self.fetch(request) as? [Pet] if let pets = result { for pet in pets { if let events = pet.events { if let evts = events.allObjects as? [Event] { return evts // Return pets events } } } } } catch {} return nil }
How would I modify this function to only return the last event (the lastest event based on
date
)? Do I have to fetch all events each time or is there a faster way to do this?
score:1
Your entity class definitions imply that you do not have an inverse relationship from Event to Pet. You should create one (there are very, very few occasions where you are better off without an inverse). You can then fetch Events directly, rather than fetching Pets and using the events
relationship. Assuming you name the inverse relationship "pet", you would use a predicate like this:
let predicate = NSPredicate(format: "pet.dbName == %@", pet)
You can then apply sort descriptors and fetch limit to return only the most recent Event for a given pet name.
Credit To: stackoverflow.com
More questions
- Fetch last record of Entities NSSet?
- How do I fetch the last element of an array in Swift?
- Core Data fetch entities with only unique attributes
- Core Data - Fetch all entities using the same field
- CloudKit Fetch First Record Only
- Swift: Sorting Core Data child entities on fetch based on Date
- How to fetch records from a specific record zone
- How to fetch NSSet from Core Data in Swift
- Fetch attributes from multiple entities in CoreData Swift
- Generic method to fetch data from different core data entities and cast result to respective classes
- How to get live value from CoreData Or know if value is change from last fetch value in iOS Swift?
- How to handle fetch requests in CoreData when you know there are no Entities saved
- Fetch Record Assets in CloudKit Using Swift
- Core Data fetch one record that has two matching fields
- Fetch multiple entities with relation between them with one NSFetchedResultController
- How to fetch relationship entities when performing a core data migration?
- How to perform conditional 'executeFetchRequest' to fetch only those entities where particular attribute is nil?
- CoreData : Fetch Multiple Entities using NSFetchedResultController
- Remove last character from string. Swift language
- How do I decode HTML entities in Swift?
- Swift: How to get substring from start to last index of character
- How to get last 4 characters of a string?
- first and last day of the current month in swift
- Remove Last Two Characters in a String
- Convert NSSet to Swift Array
- iOS 8 Auto cell height - Can't scroll to last row
- Get the last character of a string without using array?
- Generating Swift models from Core Data entities
- In Swift, what's the cleanest way to get the last two items in an Array?
- How to capture last 4 characters from NSString
More questions with similar tag
- Linking the SKS file to a Custom Class in SpriteKit
- Get Class from UITableViewCell identifier
- UIStackView Distribution Fill Equally
- How to find out which function is cased hang on main thread
- How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator
- Parse JSON array to matrix using map
- Gigya is crashing during initialization in Swift project
- Table View Cell Row Height doesnt work
- How to pass parameter of type UnsafeMutablePointer<UnsafePointer<Void>>
- Application crashes when executing NSBatchDeleteRequest with "Unknown Command Type <NSBatchDeleteRequest,..>"
- How is it I can animate the change in bar tint color of a UINavigationBar but not a UITabBar?
- How to remove UIViewControllers from hierarchy when segueing to new UIViewControllers?
- iOS App performance degrades when it shifts to efficiency cores on my iPhone
- In Apple Swift, in what case(s) would I not want an implicitly unwrapped optional?
- CGPoint stringliteral not allowed anymore in iOS 8.1