nsfetchrequest

Access FetchRequest from xcdatamodel in iOS

拟墨画扇 提交于 2021-01-27 06:31:40
问题 I'm using NSPersistentContainer to access my core data in iOS10 app and Xcode 8. Similar to Entity, I added a "UserFetchRequest" in my Coredata xcdatamodel. Below is the screen shot. How can I access the "UserFetchRequest" in the code? 回答1: Having given your fetch request a name in the Data Model Inspector on the right hand side: (in my case named "randomFetch" where yours is "UserFetchRequest"), get the model from the persistentContainer: let model = persistentContainer.managedObjectModel

How to sort Core Data array based on Date Entered

做~自己de王妃 提交于 2021-01-13 09:19:18
问题 I have a small Core Data Model which stores an array of Strings (search history). When fetching the Data I'd like to sort by the time created. I currently just have one property in my model called searchHistory is there a way I can use the following code (key: to sort the data or will I need to add a time saved to the model? let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName) request.sortDescriptors = [NSSortDescriptor.init(key: "", ascending: true)] 回答1: Yes, you need

How to sort Core Data array based on Date Entered

允我心安 提交于 2021-01-13 09:18:08
问题 I have a small Core Data Model which stores an array of Strings (search history). When fetching the Data I'd like to sort by the time created. I currently just have one property in my model called searchHistory is there a way I can use the following code (key: to sort the data or will I need to add a time saved to the model? let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName) request.sortDescriptors = [NSSortDescriptor.init(key: "", ascending: true)] 回答1: Yes, you need

How to fetch array of objects using array of ids using core data?

别说谁变了你拦得住时间么 提交于 2021-01-02 06:25:19
问题 I have an array of ids. I want to fetch core data entity for the items in the ids array. How can we implement that using NSFetchRequest ? 回答1: Use NSPredicate fetchRequest.predicate = NSPredicate(format: "itemId IN %@", itemIds) Instead of itemId set your actual id property name. Now perform a fetch with this fetch request 回答2: let hrData = self.cstProjectDetails as NSArray 来源: https://stackoverflow.com/questions/51264432/how-to-fetch-array-of-objects-using-array-of-ids-using-core-data

How to automatically reflect CoreData+iCloud Changes in SwiftUI view?

荒凉一梦 提交于 2020-12-01 11:19:26
问题 🤓 So ...I have an app with StoreData and iCloud enabled. My data is syncing between the devices but I don't ge the behaviour I want when it comes to reflecting the changes in the UI. The behaviour I want: When a user has my app open on two devices (A & B) and makes a change one one (A) of them I want the change to automatically reflect in the second (B) device UI within some reasonable time. The behaviour I currently have: When the user makes changes on one device (A) nothing ever happens on

How to validate NSPredicate before calling fetch

老子叫甜甜 提交于 2020-06-29 06:13:59
问题 I create a search NSPredicate using a custom format string. Sometimes the syntax of this string is wrong and when I execute a fetch using a NSFetchRequest with this predicate I get a NSInvalidArgumentException , for example when the key-path is incorrect. I would much rather prefer to validate this predicate (call some method that returns YES if format is ok), than have an exception thrown that crashes my app. What can I do? To rephrase the inquiry into a simple question: Can I validate the

How to validate NSPredicate before calling fetch

纵饮孤独 提交于 2020-06-29 06:13:39
问题 I create a search NSPredicate using a custom format string. Sometimes the syntax of this string is wrong and when I execute a fetch using a NSFetchRequest with this predicate I get a NSInvalidArgumentException , for example when the key-path is incorrect. I would much rather prefer to validate this predicate (call some method that returns YES if format is ok), than have an exception thrown that crashes my app. What can I do? To rephrase the inquiry into a simple question: Can I validate the

how to group by day with core data?

£可爱£侵袭症+ 提交于 2020-06-22 12:17:47
问题 I have a Entity called deal and deal has a property called date which is the time this deal object inserted into the store. and one day may have several deal s. So I want count some data group by day, I want fetch day and countofsomething like: 2013-06-03 3 2013-06-02 4 and I don't want to use sectionPath because it only put deals into section. I know I can have this done by have another property(type:string) like dayOfTheDate which is like 2013-06-03 in each object. btw, transient property

fetchRequestFromTemplateWithName variables

早过忘川 提交于 2020-03-20 18:20:52
问题 I'm using Xcode 4.2.1 and trying to use a variable. I understand in xcode 4 you need to set the variable with $variable . I have in Xcode for my fetch request the variable as $currDate and my code as NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"YYYYMMdd"]; NSDate *currDate = [NSDate date]; NSString *currDateString = [dateFormatter stringFromDate:currDate]; NSError *error; NSFetchRequest *fetchRequest = [[[DataManager sharedInstance]

fetchRequestFromTemplateWithName variables

折月煮酒 提交于 2020-03-20 18:18:06
问题 I'm using Xcode 4.2.1 and trying to use a variable. I understand in xcode 4 you need to set the variable with $variable . I have in Xcode for my fetch request the variable as $currDate and my code as NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"YYYYMMdd"]; NSDate *currDate = [NSDate date]; NSString *currDateString = [dateFormatter stringFromDate:currDate]; NSError *error; NSFetchRequest *fetchRequest = [[[DataManager sharedInstance]