Realm
Realm中有这句话:
Since Realm parses all models defined in your code at launch, they must all be valid, even if they are never used.
也就是说在启动时所有的每个类该swizzle的方法都已经处理完毕了。我第一反应是利用load方法实现,然而一查源代码里并没有load方法。。。而且据说swift现在已经不能override load方法了=。=
研究了下调用栈(故意把dynamic的属性改成任意非Object子类的class,报错后,console会输出调用栈)和源码得出结论:
Realm初始化方法中(间接)调用objc_copyClassList来获得所有类,然后对其符合要求的类进行swizzle(class_replaceMethod)
OC runtime 确实很强大。。。Realm源码还是Swift、OC和C++混编,看来真正学好iOS的路还有很长。。。
还有这句话:
Applying KVC to a collection is a great way to update objects in bulk without the overhead of iterating over a collection while creating accessors for every item.
以前我一直认为KVC一定会影响性能,官方文档里也说
Though key-value coding is efficient, it adds a level of indirection that is slightly slower than direct method invocations. You should use key-value coding only when you can benefit from the flexibility that it provides.
这里为什么说还能提高性能呢?仔细再看了下KVC文档意识到只要小心实现-replace<Key>AtIndexes:with<Key>:
是能够将遍历更改请求优化为一句SQL语句的,自然快很多。
update:
Predicate programming guide里也有这一句话,估计是类似的实现原理
If you use the Core Data framework, the array methods provide an efficient means of filtering an existing array of objects without—as a fetch does—requiring a round trip to a persistent data store.
-
Unless you need to make simultaneous writes from many threads at once, you should favor larger write transactions that do more work over many fine-grained write transactions.
-
Please note that when updating objects, nil is still considered a valid value for optional properties. If you supply a dictionary with nil property values, then these will be applied to your object and those properties will be emptied. To ensure you don’t experience any unplanned data loss, please make sure to provide only the properties you wish to update when using this method.
-
The following is not possible:
Casting between polymorphic classes (ie, subclass to subclass, subclass to parent, parent to subclass, etc.).
Querying on multiple classes simultaneously.
Multi-class containers (List and Results). -
Once the query has been executed, or a notification block has been added, the Results is kept up to date with changes made in the Realm, with the query execution performed on a background thread when possible.
-
The only time a Results is frozen is when fast-enumerating over it, which makes it possible to mutate the objects matching a query while enumerating over it:
-
Realm objects can only be accessed from the thread on which they were first created (Realm makes concurrent usage easy by ensuring that each thread always has a consistent view of the Realm. )
-
you must get a Realm instance in each thread/dispatch queue in which you want to read or write
网友评论