To introduce a framework realm for database operations, this article focuses on using the iOS platform as an example.
Realm is a cross-platform mobile database engine that supports iOS, OS X (Objective-c and Swift), and Android;
The core data engine is built by C + + and is not an ORM built on SQLite, but has a separate database storage engine;
According to the official said performance than SQLite, CoreData, and easier to use, easier to get started.
: https://realm.io/cn/
Usage Tutorial: https://realm.io/docs/objc/latest/
Accessibility tools:
1, Realm Browser: visual access to the realm database.
2. Xcode plugin: Can quickly create realm to store model objects. Https://github.com/alcatraz/Alcatraz
Start Realm Combat:
To help you better understand realm's use, here's a general introduction to Realm's related terminology and major classes:
RLMRealm : RLMRealm is the core of the framework, the access point we build the database, just like the management object context of Core Data (Managed object contexts). For the sake of simplicity, realm provides a singleton named Defaultrealm, and in this tutorial we'll just use this singleton to do what we need. Of course, we can also import an externally written realm database file, or use the "Memory instance object" (In-memory realm instance) when we don't need to save the data on the hard drive, and you can also use multiple database files at the same time.
Rlmobject : This is our custom realm data model. The behavior of creating a data model affects the structure of the database. To create a data model, we only need to inherit rlmobject and then design the properties we want to store.
relationship (Relationships) : by simply declaring a property of a rlmobject type in the data model, we can create a "one-to-many" object relationship. Similarly, with Rlmarray we can also create "many-to-one" and "many-to-many" relationships.
Write operations Transaction (write transactions) : All operations in the database, such as creating, editing, or deleting objects, must be done in the transaction . A "transaction" is a code snippet located between the Beginwritetransaction () and the commitwritetransaction () operation.
query (Queries) : To retrieve information in the database, we need to use the "retrieve" action. The simplest form of retrieval is to send a allobjects () message to a Rlmobject object. If you need to retrieve more complex data, you can also use assertions (predicates), compound queries, and result sorting.
RLMResults : This class is the class returned after any query request is executed, which contains a series of Rlmobjects objects. Similar to Nsarray, we can use subscript syntax to access it, and we can also determine the relationship between them. Not only that, it also has many more powerful features, including sorting, finding, and so on.
1. Simple Data Manipulation1.1 Preparation: Creating a data model, inheriting from Rlmobject
How to create an object: 1. normal creation; 2. Quickly create a initwithvalue from a method in the parent class Rlmobject.
Note that all required properties must be assigned before the object is added to realm, because realm has a good semantic interpretation system inside its own engine, so Objective? Many attribute attributes of C will be ignored, such as nonatomic, Atomic, strong, copy, and weak. So in order to avoid misunderstandings, it is recommended that you do not use any attribute attributes when writing a data model.
1.2 Using the Rlmrealm object to save the specified model
There are 3 ways to write, first get RLMRealm object: RLMRealm *realm = [RLMRealm Defaultrealm];
Turn on write transactions
[Realm Beginwritetransaction];
To add a model object
[Realm Addobject:stu];
Commit Write Transaction
[Realm Commitwritetransaction];
[Realm transactionwithblock:^{
[Realm Addobject:stu];
}];
[Stu createinrealm:realm withvalue:@{@ "stu_id": @22, @ "name": @ "Ma Dongmei 2" @ "Age": @666}];
1.3 Updating the specified model using the Rlmrealm object
Method 1: Update the object directly in the transaction
[Realm Beginwritetransaction];
Stu.name = @ "potatoes";
[Realm Commitwritetransaction];
Method 2: Update According to the primary key
1. Model required for operation, must implement Method + (NSString *) PrimaryKey return primary key
2. Calling a method in a transaction
[Realm ADDORUPDATEOBJECT:STU2];
Method 3: Update According to the primary key
1. Model required for operation, must implement Method + (NSString *) PrimaryKey return primary key
2. Calling a method in a transaction
[Stu createinrealm:realm withvalue:@{@ "stu_id": @22, @ "name": @ "Ma Dongmei 2" @ "Age": @666}];
1.4 Using Rlmrealm objects to delete data
- Deletes the specified object (in the transaction)
[Realm Deleteobject:stu];
Note: The model object must be obtained from the realm database, not the one you created
Rlmobject *obj = [Realm objectwithclassname:@ "Stu" forprimarykey:@2];
- Delete all objects (in a transaction)
[Realm Deleteallobjects];
1.5 Querying data using the Rlmrealm object
- This leads to the concept of chain query: On the basis of query results, make two queries, as follows:
[Stus objectswhere:@ "Address beginswith ' Beijing '"];
[Stus sortedresultsusingproperty:@ "name" Ascending:yes];
Note: The result object of the query is lazy loading, only the actual access will load the corresponding object, so, here is the paging, in fact, from all the collection page to get. Code Demo:
Rlmresults<dog *> *dogs = [Dog allobjects];
for (Nsinteger i = 0; i < 5; i++) {
Dog *dog = dogs[i];
// ...
}
Rlmresults<stu *> *stus = [Stu objectswhere:@ "name = ' Ma Dongmei '"];
Check all: [Stu allobjects]; Precautions:
1. All queries (including query and property access) are lazily loaded in Realm and can be read only when the property is accessed
2. The result of the query is not a copy of the data: Modifying the query results (in a write transaction) directly modifies the data on the hard disk.
3. Once the search is executed, RLMResults will be kept up to date
2. Supported Data Types
Supported types include: BOOL, bool, int, Nsinteger, long, long long, float, double, nsstring, NSDate, NSData, NSNumber.
The disadvantage is that collection types are not supported, and their solutions are serialized into NSData for storage or converted into rlmarray<rlmobject> for storage
3. Relationship
On a relationship
When an object holds another object, such as a person has a pet??
To multi-relationship
1. In dog, follow the specified protocol method
Rlm_array_type (Dog)
The Rlm_array_type macro creates a protocol that allows the use of rlmarray<dog> syntax.
2. In person, define the attribute
@property (nonatomic, strong) Rlmarray<dog *><dog> *dogs;
Note: Although you can assign a value of nil to the Rlmarray property, this is only used to "empty" the array, not to remove the divisor group. This means that you can always add an object to a Rlmarray property, even if it is set to nil.
Inverse relationship
Example: A man owns a dog, and a dog has a corresponding owner?
Implementation method:
1. Define attributes in Dog
@property (readonly) rlmlinkingobjects *master;
2. Implement the Protocol method, indicating the link relationship
+ (nsdictionary<nsstring *,rlmpropertydescriptor *> *) linkingobjectsproperties {
Return @{
@ "Master": [Rlmpropertydescriptor descriptorwithclass:nsclassfromstring (@ "Stu") propertyname:@ "dogs"]
};
}
not to be continued ...
Simple introduction and use of Realm database