-
An external persistent store that contains saved records.
-
A persistent object store that maps between records in the store and objects in your application.
-
A persistent store coordinator that aggregates all the stores.
-
A managed object model that describes the entities in the stores.
-
A managed object context that provides a scratch pad for managed objects.
An external persistent store that contains saved records.
A persistent object store that maps between records in the store and objects in your application.
A persistent store coordinator that aggregates all the stores.
A managed object model that describes the entities in the stores.
A managed object context that provides a scratch pad for managed objects.
Managed object

Managed object context
Persistent store coordinator
Fetch
request
Managed
object model

Getting a Managed Object Context
managed object context 之下设置NSPersistentStoreCoordinator,NSPersistentStoreCoordinator
关联NSManagedObjectModel。
NSPersistentStoreCoordinator *psc = <#Get the coordinator#>; |
NSManagedObjectContext *newContext = [[NSManagedObjectContext alloc] init]; |
[newContext setPersistentStoreCoordinator:psc]; |
NSManagedObjectContext *context = <#Get the context#>; |
NSPersistentStoreCoordinator *psc = [context persistentStoreCoordinator]; |
NSManagedObjectModel *model = [psc managedObjectModel]; |
NSEntityDescription *entity = [[model entitiesByName] objectForKey:@"<#Entity name#>"]; |
Adding a Persistent Store
NSPersistentStoreCoordinator *psc = <#Get the coordinator#>; |
NSURL *storeUrl = [NSURL fileURLWithPath:@"<#Path to store#>"]; |
NSString *storeType = <#Store type#>; // A store type, such as NSSQLiteStoreType |
NSError *error; |
if (![psc addPersistentStoreWithType:storeType configuration:nil |
URL:storeUrl options:nil error:&error]) { |
// Handle the error. |
} |
Fetch with Sorting and a Predicate
NSManagedObjectContext *context = <#Get the context#>; |
|
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" inManagedObjectContext:context]; |
[fetchRequest setEntity:entity]; |
|
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>" ascending:YES]; |
NSArray *sortDescriptors = @[sortDescriptor]; |
[fetchRequest setSortDescriptors:sortDescriptors]; |
|
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<#Predicate string#>", |
<#Predicate arguments#>]; |
[fetchRequest setPredicate:predicate]; |
|
NSError *error; |
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; |
if (fetchedObjects == nil) { |
// Handle the error. |
} |
Fetching Distinct Values 不重复的值
NSManagedObjectContext *context = <#Get the context#>; |
|
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" inManagedObjectContext:context]; |
|
NSFetchRequest *request = [[NSFetchRequest alloc] init]; |
[request setEntity:entity]; |
[request setResultType:NSDictionaryResultType]; |
[request setReturnsDistinctResults:YES]; |
[request setPropertiesToFetch:@[@"<#Attribute name#>"]]; |
|
// Execute the fetch. |
NSError *error; |
id requestedValue = nil; |
NSArray *objects = [context executeFetchRequest:request error:&error]; |
if (objects == nil) { |
// Handle the error. |
} |
Fetching Attribute Values that Satisfy a Given Function
NSManagedObjectContext *context = <#Get the context#>; |
|
NSFetchRequest *request = [[NSFetchRequest alloc] init]; |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" inManagedObjectContext:context]; |
[request setEntity:entity]; |
|
// Specify that the request should return dictionaries. |
[request setResultType:NSDictionaryResultType]; |
|
// Create an expression for the key path. |
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"<#Key-path for the property#>"]; |
|
// Create an expression to represent the function you want to apply |
NSExpression *expression = [NSExpression expressionForFunction:@"<#Function name#>" |
arguments:@[keyPathExpression]]; |
|
// Create an expression description using the minExpression and returning a date. |
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; |
|
// The name is the key that will be used in the dictionary for the return value. |
[expressionDescription setName:@"<#Dictionary key#>"]; |
[expressionDescription setExpression:expression]; |
[expressionDescription setExpressionResultType:<#Result type#>]; // For example, NSDateAttributeType |
|
// Set the request's properties to fetch just the property represented by the expressions. |
[request setPropertiesToFetch:@[expressionDescription]]; |
|
// Execute the fetch. |
NSError *error; |
id requestedValue = nil; |
NSArray *objects = [context executeFetchRequest:request error:&error]; |
if (objects == nil) { |
// Handle the error. |
} |
else { |
if ([objects count] > 0) { |
requestedValue = [[objects objectAtIndex:0] valueForKey:@"<#Dictionary key#>"]; |
} |
} |
Creating a Managed Object
NSManagedObjectContext *context = <#Get the context#>; |
<#Managed Object Class#> *newObject = [NSEntityDescription |
insertNewObjectForEntityForName:@"<#Entity name#>" |
inManagedObjectContext:context]; |
NSManagedObjectContext *context = <#Get the context#>; |
NSError *error; |
if (![context save:&error]) { |
// Handle the error. |
} |
Deleting a Managed Object
NSManagedObject *aManagedObject = <#Get the managed object#>; |
NSManagedObjectContext *context = [aManagedObject managedObjectContext]; |
[context deleteObject:aManagedObject]; |
NSError *error; |
if (![context save:&error]) { |
// Handle the error. |
} |