NSManagedObjectContext *moc = [self managedObjectContext]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; [request setEntity:entityDescription]; // Set example predicate and sort orderings... NSNumber *minimumSalary = ...; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary]; [request setPredicate:predicate]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; [sortDescriptor release]; NSError *error; NSArray *array = [moc executeFetchRequest:request error:&error]; if (array == nil) { // Deal with error... }
NSNumber *minimumSalary = ...; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary]; NSArray *employees = [Employee findAllSortedBy:@"firstName" asceding:YES withPredicate:predicate];
#import "CoreData+ActiveRecordFetching.h"
[ActiveRecordHelpers setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"MyProject.sqlite"];
NSManagedObjectContext *myNewContext = [NSManagedObjectContext newContext];
@interface Song : NSManagedObject
@property (nonatomic, retain) NSNumber * length;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * id;
@end
#import "Song.h"
@implementation Song
@dynamic length;
@dynamic name;
@dynamic id;
@end
NSArray *songs = [Song findAll];
NSArray *songsSorted = [Song findAllSortedByProperty:@"name" ascending:YES];
Song *song = [Song findFirstByAttribute:@"name" withValue:@"Imagine"];
NSArray *songs = [Song findAllWithPredicate:peopleFilter];
Song *song = [Song createEntity];
[song deleteEntity];
Song *song = [Song createEntity];
song.name = "stairway to heaven";
song.length = [NSNumber numberWithInt:150];
[[NSManagedObjectContext defaultContext] save];
Source: https://habr.com/ru/post/130262/