nsfetchrequest

NSSortDescriptor sorting using NSDate in Swift

大城市里の小女人 提交于 2019-12-12 09:30:20
问题 How would I sort a NSFetchRequest with that the date property of the managed object. So that it creates a array with the dates going in order? Here is my code so far... var request : NSFetchRequest = NSFetchRequest(entityName: "History"); request.predicate = NSPredicate(format: "counts = %@", true) request.sortDescriptors = [???] <- What should I put here? var results : [NSManagedObject] = context.executeFetchRequest(request, error: nil) as [NSManagedObject] 回答1: // newest first request

Nested core data fetch

北慕城南 提交于 2019-12-12 02:45:31
问题 I have a core data DB with multiple entities. There is a parent entity called "Person" which has many "Friends", "Friends" have many "Activities", and "Activities" have "Types" ("Types has a to-many relationship to "Activities"). What I'm trying to achieve is filtering all the "Person" entities by "Types". A user would be tapping on a "Type" and then I would refresh my table and filter the "Person" entities that are displayed by the "Types" that are associated with them. Currently I'm

NSFetchRequest group by month

不问归期 提交于 2019-12-12 01:54:44
问题 In my model, I have a Sale entity, each Sale entity has a date property (NSDate). I want to compute the sum of sales for each month. I know how to create the expression for computing the sum, what I want to find out is how do I tell NSFetchRequest to group by month. 回答1: You can use a NSFetchedResultsController to do the grouping for you. It has a parameter in the factory method called sectionNameKeyPath which you can use to separate your data. You will get all the NSIndexPath arithmetic for

Core data iterate over fetchrequest in chunks with setFetchLimit only processing half the records

瘦欲@ 提交于 2019-12-12 00:51:13
问题 I am trying to process a lot of objects in chunks of a certain size (batchSize). This loop seems to work, but it processes only half the records. Relevant piece of code is: { //Prepare fetching products without images in the database NSFetchRequest * productFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Product"]; //Sort by last changed photo first NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"photoModificationDate" ascending:NO]; [productFetchRequest

Using KVC in NSSortDescriptor

喜你入骨 提交于 2019-12-11 18:09:38
问题 I need to sort a bunch of objects based on an integer that is stored in an NSString . I know this is one solution and it works: NSSortDescriptor *mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"from" ascending: YES comparator:^(id obj1, id obj2) { if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult) NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult) NSOrderedAscending; } return (NSComparisonResult)

NSFetchResultsController + sectionNameKeyPath + section order

不羁的心 提交于 2019-12-11 17:35:59
问题 I have a one to many entity relationship between two entities: EntityP (Parent) <-->> EntityC (Child) Attributes and Relationships: EntityP.title EntityP.dateTimeStamp EntityP.PtoC (relationship) EntityC.title EntityC.dateTimeStamp EntityC.CtoP (relationship) // Can be used to get "one" EntityP record I use fetch results controller to show the results. Here's my implementation of the fetch results controller: #pragma mark - #pragma mark Fetched results controller - (NSFetchedResultsController

UITableView sectioned with sort order not sectioning correctly

巧了我就是萌 提交于 2019-12-11 16:31:06
问题 So I am trying to get a UITableView to show a list of objects by section (thing.title), but list them in descending order by date. The table is is split into sections, which are labeled correctly (section headers are the different thing titles). But the objects in each section are only half correct. The objects in each section are listed in descending order, but some sections contain data that should be in other sections. An example of what is happening: <Header> Big Title Name <data><Big

Using NSPredicate how to filter value from core data using 1 entity

不羁岁月 提交于 2019-12-11 15:07:15
问题 I have one entity & three Attribute called as item, rate, category. i want to filter value from category attribute. if (managedObjectContext==nil) { AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; managedObjectContext = appDelegate.managedObjectContext; } NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Restaurantmenu" inManagedObjectContext:managedObjectContext]; [request

Predicate works for array but not for fetchRequest

戏子无情 提交于 2019-12-11 08:28:33
问题 I am trying to create a fetchRequest that shows me all values that are not already in another array. This code returns the array I expect: NSArray *questionChoices = [self.currentQuestionChoiceGroup.questionChoices allObjects]; NSArray *filteredQuestionChoices = [questionChoices filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NONE %@ == code", myarr]]; myarr contains 1 item, and that 1 item is excluded from the filtered results, as expected. However, this code doesn't work:

How to use the “IN” Operator with a Predicate

与世无争的帅哥 提交于 2019-12-10 22:42:30
问题 How to write NSPredicate to fetch the rows that matches with values in the array? E.g: [NSPredicate predicateWithFormat:@"UserName IN %@",UserIDArray]; If UserIDArray contains ID of users, how does one fetch the user name which matches with the values in this array? 回答1: Let me start with this advice. Core Data is not SQL. Entities are not tables. Objects are not rows. Columns are not attributes/properties. Core Data is an object graph management system that may or may not persist the object