Direct use of

Goto –> Github

Start reading

To solve the pain points

Pain points1.Duplicate code, originally add delete change check general two or three lines, submit accounted for2[[RLMRealm defaultRealm] beginWriteTransaction];// modify the database
[[RLMRealm defaultRealm] commitWriteTransaction];
Copy the code
Pain points2.This experience is better than the above one, but there are more block closures and many drawbacks, such as the loss of directnessreturnThe convenience of the method increases the process of passing values across domains, especially weak references, which increase the risk of circular references unknowingly. You also need to retrieve realm objects repeatedly inside blocks. [[RLMRealm defaultRealm] transactionWithBlock:^{// modify the database
}];
Copy the code

What functionality needs to be encapsulated?

  1. Turn on or off write transactions
  2. How to get a Realm object
  3. Write transactions avoid nesting

Implement write transaction on/off

  • Use the cleanup attribute to automatically execute the function at the end of the scope to automatically close write transactions without interfering with returns and without passing values across scopes. This is a very high lever point technique.

Implementation of a realm object access method

  • Use macro definitions to easily provide fixed realm variables to scopes

Implement write transactions to avoid nesting

  • Macros don’t work beautifully with this kind of complex judgment. We need to introduce custom types, which are responsible for recording nesting and then encapsulating the corresponding case.

RealmLess results achieved

  • A simple piece of modified code is shown below to show that we have basically achieved the goal of only caring about the modification process.
{ @realm_writing_scope
   person.name = @"10JQKA";
}
Copy the code

In particularly complex situations, RealmLess’s improvement in production efficiency is even more obvious

{ @realm_writing_scope_with(OtherRealmObject)
	[rll_realm deleteObject:obj1];
	{ @realm_writing_scope_with(rll_realm)
   		[rll_realm addObject:obj2]; /// RealmLess changes take effect immediately when nested} obj2 = [Obj2 objectWhere:...] ; }Copy the code