Use of realm Database (ii) database additions, deletions, modifications, queries

Source: Internet
Author: User

Increase:
// Create Object Person *author = [[Person alloc] Init];author.name    @ "David Foster Wallace" ; // Get the default Realm RLMRealm *realm = [RLMRealm Defaultrealm]; // You are need to does this once (per thread) // ADD to Realm with transaction [Realm Beginwritetransaction]; [Realm Addobject:author]; [Realm Commitwritetransaction];
Delete:
// Book stored in Realm // Delete A object with a transaction [Realm Beginwritetransaction]; [Realm Deleteobject:cheesebook]; [Realm Commitwritetransaction]; // Delete all objects from the realm [Realm Beginwritetransaction]; [Realm Deleteallobjects]; [Realm Commitwritetransaction];

Modify:
//Update An object with a transaction[Realm Beginwritetransaction];author.name=@"Thomas Pynchon"; [Realm Commitwritetransaction];//Creating a book with the same primary key as a previously saved bookBook *cheesebook =[[Book Alloc] Init];cheesebook.title=@"Cheese Recipes"; Cheesebook.price= @9000; Cheesebook.ID= @1;//Updating Book with id = 1[Realm Beginwritetransaction]; [Book Createorupdateinrealm:realm Withvalue:cheesebook]; [Realm Commitwritetransaction];

Inquire:

The Realm object query returns a Rlmresults object. It contains a series of rlmobject. The Rlmresults has a interface (interface) that is very similar to Nsarray and the object can be obtained by indexing (index) subscript. But unlike Nsarrays, Rlmresult is classified-it can only hold one type of rlmobjects

Get objects by type

The most basic way to get an object from realm is [RLMObject allObjects] to return a rlmresults, which is all rlmobject instances of the subclass of the query.

// Query the default Realm // retrieves all Dogs from the default Realm // Query a specific Realm RLMRealm *petsrealm = [RLMRealm realmwithpath:@ "pets.realm"//  get a specific Realm//  Retrieve all Dogs from the That realm
predicate/Condition Query

If you are familiar with Nspredicate, then you already know how to query in realm. Rlmobjects,rlmrealm, Rlmarray and rlmresults all provide a good way to query a particular rlmobjects: simply pass nspredicate instance, predicate string, predicate format string, You can get the Rlmobjects instance you want. Just like the nsobject.

For example, the following code is an extension of the above. by calling [Rlmobject Objectswhere:], all the colors in the default realm database are khaki, with the name beginning with the "B" instance of the dog.

//Query using a predicate stringRLMResults *tandogs = [Dog objectswhere:@"color = ' tan ' and name Beginswith ' B '"];//Query using an Nspredicate objectNspredicate *pred = [Nspredicate predicatewithformat:@"color =%@ and name Beginswith%@",                                                     @"Tan",@"B"];tandogs= [Dog objectswithpredicate:pred];

See Apple's predicates Programming Guide for more information on how to create predicates

Conditional ordering

In many cases, we would like to get or query the results returned by a certain criteria to sort. Therefore, Rlmarray supports sorting data columns using the specified properties. Realm allows you to specify a sort requirement and sort by one or more attributes. For example, the following code calls the [RLMObject objectsWhere:where:] order of the returned data "dogs", sorted by the name of the alphabetical table ascending:

// Sort Tan Dogs with names starting with "B" by name RLMResults *sorteddogs = [[Dog objectswhere:@ 'color = ' tan ' and name Beginswith ' B ']                               Sortedresultsusingproperty: @" name [ Ascending:yes];
Chained query

A unique attribute of the realm query engine is its ability to perform simple and fast chained queries without the hassle of traditional databases.

For example, if you want the result sequence of all tawny puppies, then look for a puppy whose name starts with "B". You can send the following request.

RLMResults *tandogs = [Dog objectswhere:@ "color = ' tan '"*tandogswithbnames = [ Tandogs objectswhere:@ "name Beginswith ' B '"];

For more information, please refer to the official website: realm-objective-c

Use of realm Database (ii) database additions, deletions, modifications, queries

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.