IOS-CoreData database management tool!

Source: Internet
Author: User

1. Previous Article

Data management was implemented with SQLite3 last time, And CoreData was used in this preparation.

Core Data is a powerful framework in the iOS SDK. It allows programmers to store and manage Data in an object-oriented manner. With the Core Data framework, programmers can easily and effectively manage Data through object-oriented interfaces.

Compared with SQLite3, using CoreData is more helpful for programmers to manage data,

In addition to the preparation at the beginning, the subsequent operations are very convenient.

In addition, this is similar to the hibernate framework in JAVA.

Next, let's talk about its implementation steps.

 

  

Table Structure: NSEntityDescription

Table Record: NSManagedObject

Database storage method: NSPersistentStoreCoordinator (persistence Storage Coordinator)

Database Operation: NSManagedObjectContext (managed object context)

 

  

      NSManagedObjectModel *model =               NSPersistentStoreCoordinator *store =               NSError *error =     NSURL *url = [               [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&           (error ==         NSLog(                           _sharedContext =                           _sharedContext.persistentStoreCoordinator =     }          NSLog(     }

Add:

1. Create an INST object (insert)

    Person *p = [[Person alloc] init]

 

2. Set Object Attributes

      person.name =     person.phoneNo =     person.qq =     person.weibo = _weiboText.text;

 

3. Save the context

            [NSEntityDescription insertNewObjectForEntityForName:           NSManagedObjectContext *context =                    NSLog(                        }          NSLog(     }

 

 

Update:

1. Determine whether the same model already exists

      Person *person =     (person === [NSEntityDescription insertNewObjectForEntityForName:

 

2. Set Object Attributes

   person.name ==== _weiboText.text;

 

3. Save the context

                   NSLog(                        }          NSLog(     }

 

 

Delete

        Person *person = 

 

          NSManagedObjectContext *context =                                                           NSLog(         }              NSLog(         }
Iii. Query

1. Use NSFetchedResultsController

    NSFetchedResultsController *_fetchedResultsController;

 

2. Monitor the change of the managed object context object and report it to delegate

       _fetchedResultsController. = self;

 

2.1 when the content of the operation data context changes, the proxy method of the capture result controller is automatically called.

Mark query result controller proxy method-() controllerDidChangeContent :( NSFetchedResultsController *}

 

3. Create a controller

Generally, you will create an NSFetchedResultsController instance as a member variable of tableview. During initialization, you provide four parameters:

1. A fetchrequest must contain a sortdescriptor to sort the result set.

2. A managedobject context. The controller uses this context to execute the data retrieval request.

3. An optional keypath is used as the sectionname. The controller uses keypath to split the result set into multiple sections. (Nil indicates that there is only one section)

4. The name of a cachefile used to buffer data and generate section and index information.

  NSFetchRequest *reqest = _fetchedResultsController.fetchRequest;

 

      >      >      >      >      */     _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];

 

 

 

5. Note: The capture request must be executed. The returned data is contained in the sections. All objects in this array comply with the NSFetchedResultsSectionInfo protocol. You can use numberOfObjects to obtain the number of data objects in a group.

    [_fetchedResultsController.sections[] numberOfObjects];

 

 

Clear Saup

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.