nsfetchrequest

fetch core data string and place in a label (Swift4)

跟風遠走 提交于 2019-11-28 12:44:07
问题 I am trying to call 2 different core data strings and place them each on separate labels. Right now I am getting the error Cannot invoke initializer for type 'init(_:)' with an argument list of type '([NSManagedObject])'. This error is coming from j1.text = String(itemsName). I added both view controllers for saving and displaying. import UIKit import CoreData class ViewController: UIViewController { @IBOutlet var j1 : UITextField! @IBOutlet var j2 : UITextField! @IBAction func save(){ let

CoreData get distinct values of Attribute

烂漫一生 提交于 2019-11-28 03:20:45
I'm trying to setup my NSFetchRequest to core data to retrieve the unique values for a specific attribute in an entity. i.e. an entity with the following information: name | rate | factor | _______|______|________| John | 3.2 | 4 | Betty | 5.5 | 7 | Betty | 2.1 | 2 | Betty | 3.1 | 2 | Edward | 4.5 | 5 | John | 2.3 | 4 | How would i set up the request to return an array with just: John, Betty, Edward? AlleyGator You should use the backing store to help you get distinct records. If you want to get an array with just John, Betty, Edward here's how you do it: NSFetchRequest *fetchRequest =

Core data, how to get NSManagedObject's ObjectId when NSFetchRequest returns NSDictionaryResultType?

时间秒杀一切 提交于 2019-11-28 03:20:31
I have an NSFetchRequest which is returning the objects' properties in an NSDictionaryResultType . Is it possible to also get the objects' ObjectId within this dictionary? Otherwise I will need to run the query with a return type of NSManagedObjectResultType which is much slower for a large number of returned items. Yes you can, using the very nifty but badly-documented NSExpressionDescription class. You need to add a properly-configured NSExpressionDescription object to the array of NSPropertyDescription objects you set via setPropertiesToFetch: for your NSFetchRequest . For example:

Fetching selected attribute in entities

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 14:55:21
I have a core-data entity with several attributes, and I want a list of all the objects in one attribute. My code looks like this: let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let context:NSManagedObjectContext = appDel.managedObjectContext! let sortDesc = NSSortDescriptor(key: "username", ascending: true) let fetchReq = NSFetchRequest(entityName: "Identities") fetchReq.sortDescriptors = [sortDesc] fetchReq.valueForKey("username") let en = NSEntityDescription.entityForName("Identities", inManagedObjectContext: context) userList = context

How to get Core Data object from specific Object ID?

此生再无相见时 提交于 2019-11-27 10:03:49
I can easily get an object's ID in Core Data using the following code: NSManagedObjectID *moID = [managedObject objectID]; However, is there a way to get an object out of the core data store by giving it a specific object ID? I know that I can do this by using an NSFetchRequest, like this: NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:managedObjectContext]; [fetchRequest setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(objectID = %@)", myObjectID];

iOS: natural sort order

*爱你&永不变心* 提交于 2019-11-27 08:19:50
问题 I have an app for iOS that uses Core Data to save and retrieve data. How would I fetch data sorted by a field of NSString type in natural sort order ? Right now the result is: 100_title 10_title 1_title I need: 1_title 10_title 100_title 回答1: You can use localizedStandardCompare: as selector in the sort descriptor for the Core Data fetch request, for example NSSortDescriptor *titleSort = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(localizedStandardCompare:)

NSFetchRequest and predicateWithBlock

纵饮孤独 提交于 2019-11-27 04:13:49
I am playing with an app that uses Core Data and NSManagedObjects to populate a UITableView. There is only one class in my application, called Event . I have created the following custom instance method on Event : - (BOOL)isExpired { return ([[self.endOn dateAtEndOfDay] timeIntervalSinceNow] < 0); } I would like to limit the UITableView that displays Event objects to only the Events that are expired - that is, where isExpired returns YES . I have tried to do this by adding an NSPredicate to the NSFetchRequest : NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject,

How to get Core Data object from specific Object ID?

孤街浪徒 提交于 2019-11-26 17:54:04
问题 I can easily get an object's ID in Core Data using the following code: NSManagedObjectID *moID = [managedObject objectID]; However, is there a way to get an object out of the core data store by giving it a specific object ID? I know that I can do this by using an NSFetchRequest, like this: NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:managedObjectContext]; [fetchRequest

Fetching selected attribute in entities

主宰稳场 提交于 2019-11-26 16:57:44
问题 I have a core-data entity with several attributes, and I want a list of all the objects in one attribute. My code looks like this: let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let context:NSManagedObjectContext = appDel.managedObjectContext! let sortDesc = NSSortDescriptor(key: "username", ascending: true) let fetchReq = NSFetchRequest(entityName: "Identities") fetchReq.sortDescriptors = [sortDesc] fetchReq.valueForKey("username") let en =