IOS development series: One way to convert CoreData Object to Fault

Source: Internet
Author: User

IOS development series: One way to convert CoreData Object to Fault

@ Quote:

Recently I have been dealing with CoreData. This is an iOS component with a huge architecture and steep learning curve. Every time I encounter a problem, I will have a new understanding of it.

This time I will only talk about the Fault problem caused by the reference RELATIONSHIP BETWEEN THE ERROR awareness Object (NSManagedObject) and Context (NSManagedContext.

If you have used CoreData, you should have a direct understanding of its basic usage. That is, all objects are stored in the Context. At the same time, the Object also has a "managedObjectContext" method, you can get the Context where it is located.

At the beginning, I naturally thought that Context strongly references objects, and the Object references Context in turn. In fact, this is not the case. The Object's "managedObjectContext" is a method rather than a property, which has no strong reference relationship.

Assume that you create a new Context from CoreData Stack, and then extract an Object from this Context, assign this Object to a ViewController, and finally perform some tasks in this ViewController, you will soon find that the Object becomes fault and cannot be used.

This is because if this Context is not strongly referenced, after it is released, the Object will be "Fault cannot be fulfilled" because it does not have this Context, and thus cannot be used.

For "Fault cannot be fulfilled", there is another example in the official Apple document "Troubleshooting Core Data", which is not the same as what I encountered. It describes that after an Object is deleted from the Context, the actual content has been deleted, but the Object has not been released during the memory management period, only a Fault is returned. If you access this Object again, the error "Fault cannot be fulfilled" will be returned.

Back to the problem I encountered, usually in a program that is not particularly complex based on CoreData, we will not use many Context, in addition, CoreData Stack strongly references Context at the AppDelegate level. In particular, it does not use a newly created Context and forgets to strongly reference it. Therefore, this problem is too uncommon.

In short, you must differentiate method and property when reading documents. Even if you use the same syntax + return the same thing, you cannot think of them as the same thing. From this point of view, it is necessary to strictly use the [object method] and object. property syntax.

Certificate certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PS:I have read the API documentation and I have never understood the following:

-(BOOL) isFault

Description

Returns a Boolean value that indicatesWhether the specified er is a fault.


Knowing whether an object is a fault is useful in many situations when computations are optional. it can also be used to avoid growing the object graph unnecessarily (which may improve performance as it can avoid time-consuming fetches from data stores ).

If this method returns NO, then the specified er's data must be in memory. however, if this method returnsYES, it does not imply that the data is not in memory. the data may be in memory, or it may not, depending on login factors influencing caching

If the caller is a fault, calling this method does not cause it to fire. // do not really understand this sentence? Please enlighten me @@



Related Article

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.