Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to check an coredata object is nil

I want to find out the objects in the core data, my code:

Types:

signedDate (Date)

alarmDate(Date)

starTime(NSDate)

endTime(NSDate)

NSString *str = @"(signedDate >= %@) AND (signedDate < %@) AND (alarmDate == nil)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:str, starTime, endTime];
[request setPredicate:predicate];
NSArray *results=[context executeFetchRequest:request error:nil];

predicate is wrong ? How to judge an coredata object is nil? What the predicate should be?

like image 656
yjw44 Avatar asked Apr 20 '12 12:04

yjw44


People also ask

How do I clear my Core Data?

One approach to delete everything and reset Core Data is to destroy the persistent store. Deleting and re-creating the persistent store will delete all objects in Core Data.

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What is fault in Core Data?

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: A managed object fault is an instance of the appropriate class, but its persistent variables are not yet initialized.

Where is Core Data stored?

The persistent store should be located in the AppData > Library > Application Support directory.


2 Answers

I don't think the predicate syntax calls for == nil. Use only one =

NSString *str = @"(signedDate >= %@) AND (signedDate < %@) AND (alarmDate = nil)";

Your code above works right. It should be YES since it's nil.

BOOL ok;
predicate = [NSPredicate predicateWithFormat:@"firstName = nil"];
ok = [predicate evaluateWithObject:[NSDictionary dictionary]];

ok = [predicate evaluateWithObject:
[NSDictionary dictionaryWithObject:[NSNull null] forKey:@"firstName"]]; 
like image 173
mprivat Avatar answered Oct 05 '22 17:10

mprivat


From apple docs

Testing for Null

If you want to match null values, you must include a specific test in addition to other comparisons, as illustrated in the following fragment.

predicate = [NSPredicate predicateWithFormat:@"(firstName == %@) || (firstName = nil)", firstName];
filteredArray = [array filteredArrayUsingPredicate:predicate];
NSLog(@"filteredArray: %@", filteredArray);

// Output:
// filteredArray: ( { lastName = Turner; }, { birthday = 1972-03-23 20:45:32 -0800; firstName = Ben; lastName = Ballard; }

By implication, a test for null that matches a null value returns true. In the following code fragment, ok is set to YES for both predicate evaluations.

BOOL ok;
predicate = [NSPredicate predicateWithFormat:@"firstName = nil"];
ok = [predicate evaluateWithObject:[NSDictionary dictionary]];

ok = [predicate evaluateWithObject:
    [NSDictionary dictionaryWithObject:[NSNull null] forKey:@"firstName"]];
like image 38
shawnwall Avatar answered Oct 05 '22 18:10

shawnwall