Realm — the best solution to simplify the real write transaction in IOS

Time:2020-11-28

Realm — the best solution to simplify the real write transaction in IOS(other platforms can also learn from this idea)

Direct use

Goto –> Github

Start reading

Solve the pain point

Pain point 1, duplicate code, originally add, delete, modify check general two or three lines, submit accounted for 2 lines, basically a half more code
[[RLMRealm defaultRealm] beginWriteTransaction];
    ///Modify database
[[RLMRealm defaultRealm] commitWriteTransaction];

The experience of pain point 2 is better than the above method, but there are many defects due to the block closure. For example, direct return cannot be returned, cross domain value transfer, weak reference also has redundant code amount, and it is necessary to repeatedly obtain the realm object inside the block.
[[RLMRealm defaultRealm] transactionWithBlock:^{
    ///Modify database
}];

 

What functions need to be encapsulated?

  1. On and off of write transactions
  2. How to get the realm object
  3. Write transactions to avoid nesting

Enable and close write transactions

  • By using the function that cleanup property automatically executes at the end of scope, the write transaction can be closed automatically without blocking return and cross effect value transfer.

How to get real object

  • It is easy to provide fixed range variables to the scope by using macro definition

Implement write transactions to avoid nesting

  • This kind of complex judgment macro can not solve the problem gracefully. It needs to introduce a specific object, which is mainly responsible for whether records are nested or not, and then deal with the corresponding situation.

Effect of realless

{ @realm_writing_scope
   person.name = @"FKJJ";
}

In particularly complex situations, the improvement of production efficiency by realless is more obvious

{ @realm_writing_scope_with(OtherRealmObject)
    [rll_realm deleteObject:obj1];
    { @realm_writing_scope_with(rll_realm)
           [rll_ realm  addObject:obj2 ]; // / realless takes effect immediately when nested
    }
    obj2 = [Obj2 objectWhere:...];
}

 

Other platforms can also use this idea to implement destructors.

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]