Use of realm database (i) Simple introduction of databases and creation of models

Source: Internet
Author: User

Realm is a mobile database that can replace SQLite and CoreData

Usage Prerequisites:
    • IOS >= 7 or Mac OS X >= 10.9
    • Xcode >= 6
    • Both Objective-c & Swift is supported.
Installation:

Can be added to the project via Cocoapods

Pod serarch ' Realm '

Realm Browser/Database Manager

Tools to view and browse data

Xcode plugin:

A plugin that can quickly create a realm model

Model:

The Realm data model is actually the traditional OC class, but it needs to inherit from Rlmobject instead of NSObject

A simple person class model

1 #import<Realm/Realm.h>2 3 @interfacePerson:rlmobject4@property NSString *name;5@propertyintAge ;6@property NSDate *birthdate;7 @end8 9 //This protocol enables typed collections. i.e.:Ten //rlmarray<person> OneRlm_array_type (person)

Type of attribute (property)

Realm supports the following property types: bool, bool, int, Nsinteger, long, float, double, cgfloat, NSString, NSDate, and NSData.

You can also use RLMArray\<_Object_\> and RLMObject to simulate one or more of the relationships--realm also supports RLMObject inheritance.

Property attribute (attributes)

Please note that realm ignores Objective-c's property attributes, such as nonatomic, Atomic, strong, copy, weak, and so on. Therefore, do not use any property attributes when writing to the model. However, if you set it up, these attributes will remain in effect until it RLMObject is written to the realm database. RLMObjectnames that are customized for getter and setter work, whether in or out of realm

Data Model Customization
  • +attributesForProperty:A property value (attrbutes) that can be overridden to provide a specific property, such as a property value to add an index.
  • @interfacefloat*title; @end @implementation book + (Nsarray *) indexedproperties {  return @[@ "title" ];} @end

  • +defaultPropertyValuesCan be overridden to provide a default value for the newly created object.
  •   @interface   Book:rlmobject@property  float   price; @property NSString  * title;   @end   @implementation   book  + (nsdictionary *return  @{@ " price  : @0 , @"  title   ":  };}   @end  

     

  • +primaryKeyCan be overridden to set the primary key for the model. Defining a primary key can improve efficiency and ensure uniqueness.
  • @interfaceID*name; @end @implementation person + (NSString *) PrimaryKey {    return@ "ID" ;} @end

  • ignoredPropertiesCan be overridden to prevent realm storage model properties.
  • @interfaceperson:rlmobject@property Nsinteger tmpid; @property (ReadOnly) NSString *name;//read-only Properties is automatically ignored@property NSString *firstName, @property nsstring*LastName;@end@implementation Person+ (Nsarray *) ignoredproperties {return@[@"Tmpid"];}-(NSString *) name {return[NSString stringWithFormat:@"%@ %@", Self.firstname, Self.lastname];}@end
    Storage objects
  • Rrealm objects can be instantiated and used alone, as are other regular objects. If you want to share or permanently save in multiple threads to reuse objects, you must store them in the realm database-this must be done in a write transaction. You can add an object with the following code:
  •  //  Create object  person *author =< Span style= "color: #000000;" > [[Person alloc] init];author.name  = @ "   ;  //  Get the default Realm  RLMRealm *realm = [RLMRealm Defaultrealm];  //  "need to does this once (per thread)  //  Add to Realm with transaction  [realm beginwritetransaction]; [Realm Addobject:author]; [Realm Commitwritetransaction];  

    When you add this object to the realm, you can share it in multiple threads. And from now on, every change made (must be done in a write transaction) will also be stored permanently. When the write transaction is complete, this change will be visible to all threads that share the realm database.

    It is important to note that write operations are blocked from each other and that their corresponding processes are affected. This is the same as other permanent data storage solutions, so it is recommended that you use common and most efficient scenarios to put all writes into a single process.

    Also note that because of the MVCC structure of realm, reading is not affected by a write transaction in progress.

Use of realm database (i) Simple introduction of databases and creation of models

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.