7

I am mapping Json Data from Server using Restkit and I am Displaying those data by fetching from db. There is a refresh button in my view which performs the above operation again.

Scenario: I have two tables Key & Profile which has one-one relationship. I am fetching data from DB using follwing code

NSFetchRequest *fetchRequest = [Key fetchRequest];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"Profile"]];
[fetchRequest setIncludesSubentities:YES];
NSArray *sortedObjects = [Key executeFetchRequest:fetchRequest]; 

Above array returns all object in DB. but when i check that using breakpoint, i got some core data fault which was the reason for not showing all the data.

//All data in sortedObjects are like this.

<Key: 0x889f2f0> (entity: Key; id: 0x889e400 <x-coredata://981A476D-55AC-4CB4-BBD8-E0285E522412/Key/p1489> ; data: <fault>)

Any idea

2 Answers 2

22

This might be a misunderstanding about what a 'fault' is.

As described in Apple's documentation:

Faulting is a mechanism Core Data employs to reduce your application’s memory usage.

and

A fault is a placeholder object that represents a managed object that has not yet been fully realized, or a collection object that represents a relationship:

So, if you see the word 'fault' in logs when you're working with Core Data, it doesn't mean that an error has occurred. What unexpected behaviour are you seeing in your app?

1
  • There was some caching problem in Restkit which was not caching all objects in the database. but when i upgrade my restkit, the cache problem solved. Don't know the actual reason. Thanx for ur answer.
    – iPhone Guy
    Oct 25, 2012 at 6:18
19

You haven't actually described a problem. A Core Data fault isn't an error-- it's more like a page fault in a file system. It just means that the data hasn't been read yet. What you're describing is completely normal and expected. If you access any of the attributes of the returned objects, the fault will be automatically filled and you'll see the results. So if your Key entity has an attribute called name, you can still look up the value(s) for name and even log them if you want.

You could force the faults to be filled by adding this before executing the fetch request:

[fetchRequest setReturnsObjectsAsFaults:NO];

It's not necessary though and, depending on what attributes you have and how many objects you fetch, could use a lot more memory than is really needed.

7
  • can't able to find my problem, but got an idea from ur answer.
    – iPhone Guy
    Oct 25, 2012 at 6:19
  • You haven't described any problems. A fault in Core Data isn't a problem. Is there more to it that you haven't explained? Oct 25, 2012 at 18:05
  • Actually my problem was i am using restkit framework for mapping json data from server and fetching from db etc..but there was some caching problem with restkit which is not fetching all datas from db..if i have 50 records, sometimes it fetches 20 records, sometimes if fetches all 50 records. i thought it was caching pblm. i just upgrade my restkit.it works fine now..can u suggest me any good tutorial of core data to look more into detail. Thanks
    – iPhone Guy
    Oct 26, 2012 at 4:36
  • My preference is "Core Data for iOS", which you can find at amzn.com/0321670426 (full disclosure: I co-wrote it). Marcus Zarra's book is also good. Oct 26, 2012 at 17:30
  • I second Marcus Zarra's book.
    – Adam Waite
    May 28, 2014 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.