IosCoreData framework is used to add, delete, modify, and query context data, association between tables, 1-to-many, 1-to-1, predicate query, and multi-Table connection.

Source: Internet
Author: User

IosCoreData framework is used to add, delete, modify, and query context data, association between tables, 1-to-many, 1-to-1, predicate query, and multi-Table connection.

-(void) viewDidLoad {

[super viewDidLoad];



[self _creatTable]; // Insert data



// [self _query]; // query data





// KVC is very domineering, even though readonly can be assigned via kvc, the essence of kvo

// Book * book = [[Book alloc] init];

//// book.name = @ "book1";

// [book setValue: @ "book2" forKey: @ "name"];

// NSLog (@ "% @", book.name);

}



#pragma mark-_query query

-(void) _query {

// Initialize the model

NSManagedObjectModel * model = [NSManagedObjectModel mergedModelFromBundles: nil];



// Create a basic library coordinator

NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: model];



// Get the sandbox path

NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

path = [path stringByAppendingPathComponent: @ "coreData.sqlite"];

NSLog (@ "% @", path);

NSURL * url = [[NSURL alloc] initFileURLWithPath: path];



// Load the base library path and base library type for the coordinator

[psc addPersistentStoreWithType: NSSQLiteStoreType configuration: nil URL: url options: nil error: nil];

// create context

NSManagedObjectContext * context = [[NSManagedObjectContext alloc] init];

context.persistentStoreCoordinator = psc;





// Initialize the query request

NSFetchRequest * request = [[NSFetchRequest alloc] initWithEntityName: @ "Teacher"];



// Predicate filtering (query condition)

#pragma mark '='

// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name =% @", @ "zhangks"];

// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name = 'zhangks'"];

#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 query

// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name =% @ and age = 19", @ "zhangks"];

// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name =% @ && age = 19", @ "zhangks"];



#pragma mark 'between' fuzzy query

// Way 1

// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "age between {20,30}"];



// Way 2

// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "age between% @", @ [@ "10", @ "29"]];

// predict = [NSPredicate predicateWithFormat: predict.predicateFormat];



// request.predicate = predict;



// Data sorting ascending = YES order, ascending = NO reverse order

NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey: @ "name" ascending: YES];

request.sortDescriptors = @ [sort];



NSError * error;

// execute query

NSArray * array = [context executeFetchRequest: request error: & error]; // The query result returns an array

// NSInteger count = [context countForFetchRequest: request error: & error];

// Number of query results, return nsinteger





// NSLog (@ "% li", count);



/ *

// // Obtained in KVC

// for (NSManagedObject * student in array) {

// NSLog (@ "————————————————————% @,% li", [student valueForKey: @ "name"], [[student / Users / zhangxin /Desktop/OC/UI/5.19/5.21coreData/testtestcoredata/testtestcoredata/ViewController.m valueForKey: @ "age"] integerValue]);

//}



// // Obtained by subclass KVC

// for (Student * student in array) {

// NSLog (@ "% li,% @", [student.age integerValue], [student valueForKey: @ "name"]);

//}

* /// KVC



// Obtained through the mapping object

for (Teacher * teacher in array) {

NSLog (@ "% @", teacher.name);

}





}



#pragma mark _creatTable

-(void) _creatTable {

// Initialize the model

NSManagedObjectModel * model = [NSManagedObjectModel mergedModelFromBundles: nil];



// Create a basic library coordinator

NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: model];



// Get the sandbox path

NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

path = [path stringByAppendingPathComponent: @ "coreData.sqlite"];

NSLog (@ "% @", path);

NSURL * url = [[NSURL alloc] initFileURLWithPath: path];



// Load the base library path and base library type for the coordinator

[psc addPersistentStoreWithType: NSSQLiteStoreType configuration: nil URL: url options: nil error: nil];

// create context

NSManagedObjectContext * context = [[NSManagedObjectContext alloc] 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 New NSManagedObject subclass class, automatically create a class based on the entity table in the model (inherited from NSManagedObject, with the properties and methods of NSManagedObject), so you can create it directly 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 = [[NSFetchRequest alloc] initWithEntityName: @ "Teacher"];



// Predicate filtering (query condition)

// NSPredicate * predict = [NSPredicate predicateWithFormat: @ "name = 'limei'"];



// request.predicate = predict;



NSArray * array = [context executeFetchRequest: request error: nil];

for (Teacher * teacher in array) {

Student * student1 = [NSEntityDescription insertNewObjectForEntityForName: @ "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 save), delete data stored in the cache (library table is not modified) before the delete statement is executed and not saved

// NSSet * insertSet = [context insertedObjects]; // Same as above



// save

NSError * error;

[context save: & error];

}

The use of the ios CoreData framework, the addition and deletion of the context data, the association between tables and tables, 1 to many, 1 to 1, predicate query, the use of multi-table connection ios CoreData framework, the addition and deletion of the context data Table-to-table associations, one-to-many, one-to-one, predicate query, use of multi-table connection ios CoreData framework, addition, deletion and modification of context data, table-to-table associations, one-to-many, one-to-one 1. Predicate query, use of multi-table connection ios CoreData framework, addition, deletion and modification of context data, table-to-table association, 1 to many, 1 to 1, predicate query, multi-table connection ios CoreData framework, Addition, deletion and modification of context data, table-to-table associations, one-to-many, one-to-one, predicate query, multi-table connection ios CoreData framework, addition, deletion and modification of context data, table-to-table Association, one-to-many, one-to-one, predicate query, use of multi-table connection ios CoreData framework, addition, deletion and modification of context data, table-to-table association, one-to-many, one-to-one, predicate query, multi Table connection

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.