IOS CoreData Detailed (iv) faulting and uniquing

Source: Internet
Author: User

Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc
Welcome to my iOS SDK detailed column
Http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html

Preface, faulting and uniquing are two more key concepts to understand coredata, here is a detailed explanation.
Let's look at the concept of both.

    • Faulting is a mechanism for coredata to reduce memory usage and is an inert load.
    • Uniquing is the mechanism of auxiliary faulting, which guarantees that there is only one managed object in a managed object context to express a record
Faulting limiting the size of an object graph

A fault is a placeholder for an object in memory, and the placeholder represents an object that is not fully taken into memory. are divided into two types:

    1. The fault of a managed object are objects of the related class, but the persisted stored properties of the object are not initialized
    2. An relationship fault represents an instance of the corresponding collection.

Such placeholders reduce memory usage and do not require objects related to the fault object to be taken back into memory.
For example, if an employee is taken out, the manager,department,reports by default is fault.

Fault is transparent to the consumer, CoreData automatically pulls the corresponding data from the disk when the user uses the persisted stored property of the corresponding fault object, a process known as firing faults.

The process of firing faults
    1. CoreData is first found in the cache of the persistence storage coordinator and returns if there is a process that is highly efficient
    2. If not, the fetch is executed automatically, the corresponding data is returned, and added to the cache of the persistent storage Coordinator

The following calls to ManagedObject do not cause firing faults

isEqual:isKindOfClass:,isMemberOfClass:conformsToProtocol:,respondsToSelector:, description,managedObjectContext, entity, objectID, isInserted, isUpdated, isDeleted, isFault.
About the performance optimization of faulting

There is no doubt that the efficiency of a fire one fault is very low. As an example,
An object graph for employees and departments

Remove some employees, and print the corresponding department name

for-- if you don‘t have a predicate here,error:&error];forin fetchedEmployees){    NSLog(@"%@ -> %@ department", employee.name, employee.department.name);}

This will cause firing faults as follows

JackSales [fault fires]JillMarketing [fault fires]BenjySalesGillianSalesHectorEngineering [fault fires]MichelleMarketing

Obviously, such a one-time approach is inefficient, and CoreData provides two solutions

    • Batch faulting one batch at a time
..., nil];NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self IN %@", array];
    • Pre-fetching Pre-extraction
NSManagedObjectContext *context = /* get the context */;NSEntityDescription *employeeEntity = [NSEntityDescription    entityForName:@"Employee"[[NSFetchRequest alloc] init];[request setEntity:employeeEntity];[request setRelationshipKeyPathsForPrefetching:    [NSArray arrayWithObject:@"department"]]
Convert an already initialized object to fault

There are many advantages to converting an already initialized object to fault.

    • Reduce memory usage
    • Ensure that the object's data is up to date. (Multi-threaded case)

Convert an object to fault using this method refreshObject:mergeChanges: , the corresponding persisted storage property is set to nil and the strong reference to the related object is broken.

Uniquing

As mentioned above: Uniquing is the mechanism of auxiliary faulting, which guarantees that there is only one managed object in a managed object context to express a record.
For example: Remove two employee, corresponding to the department representing fault

After the firing faulting has been performed, department is removed, and if the two are the same department, then an object is automatically pointed.

If, without this mechanism, it would cause multiple identical department to exist in memory, resulting in inconsistent objects.

IOS CoreData Detailed (iv) faulting and uniquing

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.