Core Data

  • 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.


image: Art/single_persistent_stack.jpg

Managed object
image: Art/mapping_moc_record.jpg

Managed object context

image: Art/moc_record.jpg

Persistent store coordinator

image: Art/advanced_persistent_stack.jpgFetch request

image: Art/fetch_flow_of_data.jpgManaged object model

image: Art/single_persistent_stack.jpg

Managed object model with two entities

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.
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值