Magicalrecord getting started

Source: Internet
Author: User
Magicalrecord tutorial category: iOS development 1012 people read comments (1) collect reports

Directory (?) [+]

What is magical record?

There is a technology in cocoa called core data, which is used to persist data, similar to hibernate in the Java World. In the wizard for creating a cocoa application/IOS application, you can choose whether to use core data. When it is enabled, you will find that it is in appdelegate. M adds a lot of code related to core data, but you do not know about most of the Code.

The emergence of magical record relieves this problem to some extent and lowers the threshold for using core data.

Magical record uses the active record mode in Ruby on Rails to easily add, search, and delete data. I Googled and did not find any Chinese tutorials. I recorded my trial process and wrote this article.

Install
  1. Create a project. Do not check core data in the Wizard.
  2. Download magical record and drag the magicalrecord directory to the project. Check copy items into group folder.
  3. Add coredata framework for the project. (Click the project root node, and then select targets> build phases> link binary with libraries> +> coredata. Framework> Add ).
  4. Add the header file of magical record to *-prefix. PCH:

     

    1. #import "CoreData+MagicalRecord.h"
Create a model file

The following creates a model named "person" with three fields: age, firstname, and lastname.

  1. Create a model file named "model. (File> New File... > Core data> Data Model)
  2. Click Add entity in the lower left corner to change the name of the entity to person.
  3. Add three attributes for entity: Age (integer16), firstname (string), and lastname (string ).
  4. Click editor> Create nsmanagedobject subclass... > Create creates the class corresponding to the model file.
Use magical record to initialize magical record

First, add the following code to appdelegate. m to initialize magical record:

  1. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  2. {
  3. [MagicalRecord setupCoreDataStackWithStoreNamed:@"MyDatabase.sqlite"];
  4. // ...
  5. return YES;
  6. }
  7. - (void)applicationWillTerminate:(NSNotification *)aNotification
  8. {
  9. [MagicalRecord cleanUp];
  10. }

Is it simpler than core data's default initialization?

Query records

You can use the mr_findall, mr_findallsortedby, mr_findbyattribute, and other methods of person to query person:

  1. // Find all persons in the database.
  2. NSArray *persons = [Person MR_findAll];
  3. // Search for all persons and sort by first name.
  4. NSArray *personsSorted = [Person MR_findAllSortedBy:@"firstname" ascending:YES];
  5. // Query all the person records whose age attribute is 25.
  6. NSArray *personsAgeEuqals25 = [Person MR_findByAttribute:@"age" withValue:[NSNumber numberWithInt:25]];
  7. // Search for the first record in the database
  8. Person *person = [Person MR_findFirst];
Add record

You can use the mr_createentity method of person to easily create a person. You need to use [nsmanagedobjectcontext mr_defaultcontext] mr_save] To save it:

  1. Person *person = [Person MR_createEntity];
  2. person.firstname = @"Frank";
  3. person.lastname = @"Zhang";
  4. Person. Age = @ 26; // The New llvm features are used here, And xcode 4.4 is available.
  5. [[NSManagedObjectContext MR_defaultContext] MR_save];
Update record

Directly assign values to the person found in the database, and then use nsmanagedobjectcontext to save and update the person:

  1. Person * person =...; // omitted here
  2. person.lastname = object;
  3. [[NSManagedObjectContext MR_defaultContext] MR_save];
Delete record

The mr_deleteentity of person can be used to conveniently Delete the person. The mode is the same as that for adding and updating the person:

  1. Person * person =...; // omitted here
  2. [person MR_deleteEntity];
  3. [[NSManagedObjectContext MR_defaultContext] MR_save];
TIPS: The mr_mergedobjectmodelfrommainbundle method reports an error at startup.

The core data model has a version concept. It is possible that after you initialize the magical record for the first time, you have changed the model file, resulting in an error when merging the core data model. The solution is simple. Click project> clean in the menu.

After the project uses arc, the magical record compilation fails.

Click project> build phases> compile sources, double-click the error class file, and edit compiler flags to add-fno-objc-arc.

You do not want to use the Mr _ prefix.

You only need to add a sentence # define mr_shorthand to the *-prefix. PCH file. Note that this sentence should be before # import "coredata + magicalrecord. h.

Reference link:

Magical record: How to make programming with core data pleasant

Note: magicalrecordhelpers and mrcoredataaction in this Article do not exist in the new version of magical record and have been changed to magicalrecord.

Magicalrecord getting started

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.