nsfetchrequest

fetch request for entity.attribute == @“somevalue”

折月煮酒 提交于 2019-12-03 13:28:33
How do I setup a fetch request to only pull the data from an entity's attribute with one particular value? This is the basic code I've used before. -(void)fetchResults { NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:self.entityDescription.name]; NSString *cacheName = [self.entityDescription.name stringByAppendingString:@"Cache"]; // predicate code if (self.predicate != nil) { [fetchRequest setPredicate:self.predicate]; } // end of predicate code NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; [fetchRequest

SwiftUI View and @FetchRequest predicate with variable that can change

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 09:56:06
问题 I have a view showing messages in a team that are filtered using @Fetchrequest with a fixed predicate 'Developers'. struct ChatView: View { @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Message.createdAt, ascending: true)], predicate: NSPredicate(format: "team.name == %@", "Developers"), animation: .default) var messages: FetchedResults<Message> @Environment(\.managedObjectContext) var viewContext var body: some View { VStack { List { ForEach(messages, id: \.self) { message in

Core Data: Fetch via specific property (join relationship)

女生的网名这么多〃 提交于 2019-12-03 08:31:05
I have an core data model as follows The attributes property of Page is a set of DictionaryEntry , they are values for my Page objects, much like a standard NSDictionary (except all of the keys and values are strings) I have a Page that has a DictionaryEntry with key="title" and value="Home" . How would i form a fetch request to load that specific page? You should have a look at the predicate syntax for subqueries. You can not use the usual ANY keyword as this only allows you to match one column not two at the same time. NSString *keyValue = @"title"; NSString *valueValue = @"home";

“Whole word” search in a NSString through NSPredicate

孤街醉人 提交于 2019-12-03 03:51:56
I would like to search if in the attribute description (an NSString instance) there is a given word. I tried with this predicate: [NSPredicate predicateWithFormat:@"description CONTAINS[cd] %@", theWord]; It works, but it finds also sub-words . For example, given theWord : Car it will mach also this description : A Christmas Carol Instead, I would like that my predicate will match only a , or christmas , or carol . (I'm using Core Data and NSFetchRequest.) Something like this? NSString *matchStr = @".*\\bCarol\\b.*"; NSPredicate *pred =[NSPredicate predicateWithFormat: @"description MATCHES %@

How to fetch just object IDs but also include the row data in CoreData?

血红的双手。 提交于 2019-12-03 03:47:44
In the CoreData Apple Docs on working in the background , I came across this bit of advice: For example, you can configure a fetch request to return just object IDs but also include the row data (and update the row cache) —this can be useful if you're just going to pass those object IDs from a background thread to another thread. I was wondering how you might implement this fetch request? Specifically how to update the row cache. I think this is how get just the IDs: NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"MyItem"]; // iOS 5 method request.returnsObjectsAsFaults

CoreData: Find minimum of calculated property

╄→гoц情女王★ 提交于 2019-12-03 00:49:52
Say I have a CoreData entity "Point" with two properties x and y (both NSNumber). How would a NSPredicate need to look like to let me find the closest Point to say a,b? for distance = sqrt((x-a) (x-a)+(y-b) (y-b)) While I could define a transient property that calculates a distance to a predefined point I can't see how I could programmatically change that point when launching a fetchrequest. Any help would be greatly appreciated. Sorry, but doing arithmetic in an NSPredicate isn't supported. Only comparison operators can be used. And the news gets worse. You can't use a transient property in

Can I apply multiple predicates to an NSFetchRequest? Would it be better to manually parse my results?

邮差的信 提交于 2019-12-02 17:29:34
Ok I have a basic iPad app that asks for 5 search/filter criteria from the user. Based on this data, I need to go to my core data db, and pull out any managed objects that fit that criteria. It seems like I need to apply more than one predicate to the same request, is that possible? Or could I just write a really long fancy predicate? With multiple requirements? How should I approach that? Would it be a good idea to just grab all the entities through the fetch request, and then loop through each array and grab any objects that I find that fit my search criteria? Please advise! Rog Yes it's

Checking for duplicates when importing to CoreData

孤人 提交于 2019-12-02 16:26:09
问题 I'm importing data into a Core Data store using RestKit and need to check for duplicates. If the item is already in the store, I'd like to update it with the latest attributes. If it's a new item, I'd like to create it. The import was slow so I used Instruments and saw that the longest part of importing was checking to see if the item already exists (with a fetch request) So I'd like to know if checking to see if the item is already in the store, is it faster to: use countForFetchRequest to

Checking for duplicates when importing to CoreData

蓝咒 提交于 2019-12-02 13:31:43
I'm importing data into a Core Data store using RestKit and need to check for duplicates. If the item is already in the store, I'd like to update it with the latest attributes. If it's a new item, I'd like to create it. The import was slow so I used Instruments and saw that the longest part of importing was checking to see if the item already exists (with a fetch request) So I'd like to know if checking to see if the item is already in the store, is it faster to: use countForFetchRequest to see if the item already exists, then executeFetchRequest to return the item to update or just

NSFetchRequest for groupby and count combination

柔情痞子 提交于 2019-12-02 12:31:30
I'm new in Core Data, so i want to write simple fetch request to group result by some field and also a count of another field in that group. so for example i have products table i want to retrieve all grouped products by date and the count of product, the simple query in sql is like SELECT DateRegistered,Count(*) FROM Products Group By DateRegistered Your best bet is the very flexible NSFetchedResultsController . You would have a fetch request that simply fetches the Product entity sorted by the date attribute. Please note that core data attributes should start with a small letter. You would