Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if CoreData attribute is empty

I am hoping to find a way to check, if a CoreData attribute is empty. The attribute itself is of type binary data. If the attribute is empty then I could tell my class to download and save some data into this attribute.

According to CoreData Documentation, you should not keep fetching to see if objects exists. I am wondering if there is even a way to possibly do this? without breaking this 'law'?

This is my first attempt at using CoreData. I am adding it to my code afterwards, which is slightly more painful, but as a whole so far everything seems to be going okay. I just need to figure out a logical way of checking if attribute has values. If it doesn't then I need to download and save the new data, if it does then I just use what's in the attribute.

Update :

I just found this method in the CoreData framework that I have been reading though trying to catch a break on this. Not sure if it would help.. what do you guys think?

willAccessValueForKey: Provides support for key-value observing access notification.

  • (void)willAccessValueForKey:(NSString *)key Parameters key The name of one of the receiver's properties. Discussion See didAccessValueForKey: for more details. You can invoke this method with the key value of nil to ensure that a fault has been fired, as illustrated by the following example.

[aManagedObject willAccessValueForKey:nil];

Not sure really.. the things that I dont understand is Provides support for key-value observing access notification. ???

like image 850
C.Johns Avatar asked Apr 25 '12 23:04

C.Johns


1 Answers

That notification is for when the value is going to be accessed.

If I understand you correctly, you are not wanting to see if an entity exists, but an attribute within the entity. So, I assume you have it marked as an optional attribute.

Let's say you have a binary data attribute called rawData. If you want to find all the @"MyEntity" objects in the database that do not have any data set for this attribute, you cn issue this fetch request.

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"rawData = nil"];
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:0];
like image 139
Jody Hagins Avatar answered Oct 16 '22 23:10

Jody Hagins