IOS basic database storage and iOS database storage

Source: Internet
Author: User

IOS basic database storage and iOS database storage

CoreData

Creating Model files

1. Select a template

2. Add an object

3. Add an object attribute. Note: The first letter of the attribute must be in lowercase.

1. CoreData management class (three class objects are required)

1. CoreData data operation context, responsible for all data operations, similar to the SQLite database connection handle (NSManagedObjectContext)

(1). Initialization Method

// The type of the parameter "ct"

// Three types

/*

NSConfinementConcurrencyType default value, which can be executed in a specific thread, almost no

NSPrivateQueueConcurrencyType is only executed in the main thread

NSMainQueueConcurrencyType allows execution in child threads

*/

-(Instancetype) initWithConcurrencyType :( NSManagedObjectContextConcurrencyType) ct

(2). Set persistent Storage Coordinator (attribute)

PersistentStoreCoordinator

 

2. NSManagedObjectModel)

(1). Generally, You need to obtain the URL of the model file first.

/*

Parameter 1: model file name

Parameter 2: model file suffix name

[Note]: The. xcdatamodel file in the. xcdatamodel package, which is suffixed with the data model editor and compiled into a. momd or. mom file.

*/

Method: [[[NSBundle mainBundle] URLForResource :( NSString *) name withExtension :( NSString *) ext]

(2) initialize the NSManagedObjectModel object, load the model file, and read all object information in the App.

// Parameter 1: This URL refers to the url path of the model file in (1 ).

-(Nullable instancetype) initWithContentsOfURL :( NSURL *) url;

 

3. NSPersistentStoreCoordinator)

(1). Initialization Method

-(Instancetype) initWithManagedObjectModel :( NSManagedObjectModel *) model

 

 

After talking about this, as long as we select this check box when creating the project for the above three core coreData objects, Xcode will help us build the framework. Next we only need to know how to call

 

[Note] Black indicates the class name, and red indicates an attribute method in the class.

 

2. Add data: insert an object (NSManagedObject) in the context)

1. model = [NSEntityDescription insertNewObjectForEntityForName: Object Name inManagedObjectContext: context for object management];

[Note] This model must be an NSManagedObject object. Generally, the model inherits NSManagedObject.

2. Set the attribute Value of the inserted model in Key-Value mode.

3. synchronize data to the persistent Repository: save model [NSManagedObjectContext save: nil];

 

Iii. query data

 

1. Request class pointing to a table

 

// Parameter entityName: name of the created object (called the table name in SQLite)

 

NSFetchRequest * fetchRequest = [NSFetchRequest fetchRequestWithEntityName :( NSString *) entityName];

 

 

 

2. Add query Conditions

 

(1). the condition is written to the string.

 

NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "age = 50"];

 

 

 

(2) dynamically passing values

 

/*

 

% K query field

 

% @ Query value

 

*/

 

NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "% K = % @", @ "age", @ (50)];

 

 

 

(3). Sort (NSSortDescriptor)

 

// Step 1: Initialize

 

/*

 

Parameter 1: key value, that is, the attribute for sorting

 

Parameter 2: NO indicates descending order, and YES indicates ascending order.

 

*/

 

NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey: @ "age" ascending: NO];

 

// Step 2: Add a sorting method for fetchRequest

 

FetchRequest. sortDescriptors = @ [sort];

 

 

 

(4) Range Query

 

// Range operator (two types): IN (specified value) BETWEEN (Open interval)

 

NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "age BETWEEN {15,60}"];

 

 

 

(5). String-related searches

 

/* Configure

 

String: BEGINSWITH, ENDSWITH, CONTAINS

 

Example:

 

@ "Attribute name CONTAINS [cd] 'contained string'" // CONTAINS a string

 

@ "Attribute BEGINSWITH [c] 'contained string'" // starts with a string

 

@ "Attribute name ENDSWITH [d] 'contained string'" // end with a string

 

[Note]: The contained strings must be enclosed in single quotes.

 

Note: [c] Is case insensitive.

 

[D] There is no accent without distinguishing pronunciation symbols

 

[Cd] is case-insensitive and does not distinguish between pronunciation symbols.

 

*/

 

NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "username ENDSWITH 'wu'"];

 

 

 

(6). wildcard search

 

/*

 

Wildcard: LIKE

 

Example:

 

@ "Property name LIKE [cd] '* er *'" // * indicates multiple characters

 

@ "Property name LIKE [cd] '??? Er *'"//? Represents a character

 

*/

 

NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "username LIKE '* san *'"];

 

 

 

.............. There are other condition settings. I will upload a comprehensive document for you.

 

3. Set query Conditions

 

FetchRequest. predicate = predicate;

 

 

 

4. execute the request. The request result is placed in the array, and the model is stored in the array.

 

NSArray * result = [NSManagedObjectContext executeFetchRequest: fetchRequest error: & error];

 

 

4. delete data

// Delete an object (that is, a model)

[NSManagedObjectContext deleteObject: object];

You only need to modify the model and call the save method to automatically modify the value in the database.

[NSManagedObjectContext save: nil];

 

5. Modify

Directly modify the model attribute value

Save Model

 

 

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.