Use of the ios CoreData framework, add, delete, modify, and query context data, association between tables, 1-to-many, 1-to-1, predicate query, multi-table join, ioscoredata
Here is just code, because the effect of inserting images into a blog is not very good. I wrote a summary in detail. If you are interested, you can leave an email in the comments and I will send it to you after receiving the comment.
Repost the source and pay attention to the labor achievements of the original creators. Thank you!
-(Void) viewDidLoad {
[SuperviewDidLoad];
[Self_creatTable]; // insert data
// [Self _ query]; // query data
// KVC is overbearing. It can be assigned a value even if readonly is passed through kvc. kvo is excellent.
// Book * book = [[Book alloc] init];
/// Book. name = @ "book1 ";
// [Book setValue: @ "book2" forKey: @ "name"];
// NSLog (@ "% @", book. name );
}
# Pragma mark-_ query
-(Void) _ query {
// Initialize the Model
NSManagedObjectModel * model = [NSManagedObjectModelmergedModelFromBundles: nil];
// Create the basic library Coordinator
NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel: model];
// Obtain the sandbox path
NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
Path = [path stringByAppendingPathComponent: @ "coreData. sqlite"];
NSLog (@ "% @", path );
NSURL * url = [[NSURLalloc] initFileURLWithPath: path];
// Load the basic library path and basic library type for the Coordinator
[Psc addPersistentStoreWithType: NSSQLiteStoreTypeconfiguration: nilURL: url options: nilerror: nil];
// Create a context
NSManagedObjectContext * context = [[NSManagedObjectContextalloc] init];
Context. persistentStoreCoordinator = psc;
// Initialize the query request
NSFetchRequest * request = [[NSFetchRequestalloc] initWithEntityName: @ "Teacher"];
// Predicate filtering (query condition)
# Pragma mark' ='
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name = % @", @ "zhangks"];
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name = 'hangs'"];
# Pragma mark '>'
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "age> 10"];
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name in % @", @ [@ "zhangsk", @ "jack"];
# Pragma mark 'like' fuzzy query
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name like % @", @ "zha *"];
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name like % @", @ "* zha *"];
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name like % @", @ "* zha"];
# Pragma mark 'and' and '&' fuzzy search
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name = % @ and age = 19", @ "zhangks"];
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name =%@ & age = 19", @ "zhangks"];
# Pragma mark 'between' fuzzy query
// Method 1
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "age between {20, 30}"];
// Method 2
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "age between % @", @ [@ "10", @ "29"];
// Predict = [NSPredicate predicateWithFormat: predict. predicateFormat];
// Request. predicate = predict;
// Data sorting ascending = YES, ascending = NO reverse
NSSortDescriptor * sort = [NSSortDescriptorsortDescriptorWithKey: @ "name" ascending: YES];
Request. sortDescriptors = @ [sort];
NSError * error;
// Execute the query statement
NSArray * array = [context executeFetchRequest: request error: & error]; // The returned result is an array.
// NSInteger count = [context countForFetchRequest: request error: & error];
// The number of query results. nsinteger is returned.
// NSLog (@ "% li", count );
/*
//// Obtain using KVC
// For (NSManagedObject * student in array ){
// NSLog (@ "-------------------- % @, % li", [student valueForKey: @ "name"], [[student/Users/zhangxin/Desktop/OC/UI/5.19/5.21 coreData/testtestcoredata/ViewController. m valueForKey: @ "age"] integerValue]);
//}
/// Obtain the KVC subclass
// For (Student * student in array ){
// NSLog (@ "% li, % @", [student. age integerValue], [student valueForKey: @ "name"]);
//}
* // KVC
// Obtain from the ing object
For (Teacher * teacherin array ){
NSLog (@ "% @", teacher. name );
}
}
# Pragma mark _ creatTable
-(Void) _ creatTable {
// Initialize the Model
NSManagedObjectModel * model = [NSManagedObjectModelmergedModelFromBundles: nil];
// Create the basic library Coordinator
NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel: model];
// Obtain the sandbox path
NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
Path = [path stringByAppendingPathComponent: @ "coreData. sqlite"];
NSLog (@ "% @", path );
NSURL * url = [[NSURLalloc] initFileURLWithPath: path];
// Load the basic library path and basic library type for the Coordinator
[Psc addPersistentStoreWithType: NSSQLiteStoreTypeconfiguration: nilURL: url options: nilerror: nil];
// Create a context
NSManagedObjectContext * context = [[NSManagedObjectContextalloc] init];
Context. persistentStoreCoordinator = psc;
//// Insert data
// NSManagedObject * student = [NSEntityDescription insertNewObjectForEntityForName: @ "Student" inManagedObjectContext: context];
// [Student setValue: @ "zhangks" forKey: @ "name"];
// [Student setValue: @ (19) forKey: @ "age"];
//
// NSManagedObject * teacher = [NSEntityDescription insertNewObjectForEntityForName: @ "Teacher" inManagedObjectContext: context];
// [Teacher setValue: @ "zhqo" forKey: @ "name"];
// Insert data to create the NSManagedObject subclass class. The class is automatically created based on the entity (entity) table in the model (inherited from NSManagedObject and has attributes and methods of NSManagedObject) so you can directly create it with the class name.
// Student * student = [NSEntityDescription insertNewObjectForEntityForName: @ "Student" inManagedObjectContext: context];
// Student. name = @ "jack ";
// Student. age = @ (29 );
//
//
//
// Teacher * teacher = [NSEntityDescription insertNewObjectForEntityForName: @ "Teacher" inManagedObjectContext: context];
// Teacher. name = @ "limei ";
// Initialize the query request
NSFetchRequest * request = [[NSFetchRequestalloc] initWithEntityName: @ "Teacher"];
// Predicate filtering (query condition)
// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name = 'limei'"];
// Request. predicate = predict;
NSArray * array = [context executeFetchRequest: request error: nil];
For (Teacher * teacherin array ){
Student * student1 = [NSEntityDescriptioninsertNewObjectForEntityForName: @ "Student" inManagedObjectContext: context];
Student1.name = @ "jim ";
Student1.age = @ (12 );
Student1.relationship = teacher;
NSLog (@ "% @, % @", teacher. name, student1.relationship. name );
/// Teacher. name = @ "liuwu ";
/// [Context deleteObject: teacher];
//
}
// NSSet * deleteSet = [context deletedObjects]; // Delete (not saved): delete data stored in the cache (database table not modified) before the delete statement is saved.
// NSSet * insertSet = [context insertedObjects]; // same as above
// Save
NSError * error;
[Contextsave: & error];
}
Use of the ios CoreData framework, add, delete, modify, and query context data, association between tables, 1-to-many, 1-to-1, and predicate Query, use of Multi-Table connection ios CoreData framework, addition, deletion, modification, and query of Context data, association between tables, 1-to-many, 1-to-1, predicate query, use of Multi-Table connection ios CoreData framework, addition, deletion, modification, and query of Context data, association between tables, 1-to-many, 1-to-1, predicate query, use of Multi-Table connection ios CoreData framework, addition, deletion, modification, and query of Context data, association between tables, 1-to-many, 1-to-1, predicate query, use of Multi-Table connection ios CoreData framework, addition, deletion, modification, and query of Context data, association between tables, 1-to-many, 1-to-1, predicate query, use of Multi-Table connection ios CoreData framework, addition, deletion, modification, and query of Context data, association between tables, 1-to-many, 1-to-1, predicate query, use of Multi-Table connection ios CoreData framework, add, delete, modify, and query context data, association between tables, 1-to-many, 1-to-1, predicate query, and multi-Table connection